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 tmpnam);
21 use File::Path qw(mkpath);
22 use File::Basename qw(basename dirname);
26 use POSIX qw(strftime dup2 ENOENT);
29 $SIG{'PIPE'}="IGNORE";
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,$opt_A,$opt_S,$opt_L, $opt_a);
33 my (%conv_author_name, %conv_author_email);
37 Usage: ${\basename $0} # fetch/update GIT from CVS
38 [-o branch-for-HEAD] [-h] [-v] [-d CVSROOT] [-A author-conv-file]
39 [-p opts-for-cvsps] [-C GIT_repository] [-z fuzz] [-i] [-k] [-u]
40 [-s subst] [-a] [-m] [-M regex] [-S regex] [CVS_module]
45 sub read_author_info($) {
48 open my $f, '<', "$file" or die("Failed to open $file: $!\n");
51 # Expected format is this:
52 # exon=Andreas Ericsson <ae@op5.se>
53 if (m/^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/) {
55 $conv_author_name{$user} = $2;
56 $conv_author_email{$user} = $3;
58 # However, we also read from CVSROOT/users format
60 elsif (/^(\w+):(['"]?)(.+?)\2\s*$/) {
62 ($user, $mapped) = ($1, $3);
63 if ($mapped =~ /^\s*(.*?)\s*<(.*)>\s*$/) {
64 $conv_author_name{$user} = $1;
65 $conv_author_email{$user} = $2;
67 elsif ($mapped =~ /^<?(.*)>?$/) {
68 $conv_author_name{$user} = $user;
69 $conv_author_email{$user} = $1;
72 # NEEDSWORK: Maybe warn on unrecognized lines?
77 sub write_author_info($) {
79 open my $f, '>', $file or
80 die("Failed to open $file for writing: $!");
82 foreach (keys %conv_author_name) {
83 print $f "$_=$conv_author_name{$_} <$conv_author_email{$_}>\n";
88 # convert getopts specs for use by git-repo-config
89 sub read_repo_config {
90 # Split the string between characters, unless there is a ':'
91 # So "abc:de" becomes ["a", "b", "c:", "d", "e"]
92 my @opts = split(/ *(?!:)/, shift);
93 foreach my $o (@opts) {
96 my $arg = 'git-repo-config';
97 $arg .= ' --bool' if ($o !~ /:$/);
99 chomp(my $tmp = `$arg --get cvsimport.$key`);
100 if ($tmp && !($arg =~ /--bool/ && $tmp eq 'false')) {
102 my $opt_name = "opt_" . $key;
109 chomp(my $module = `git-repo-config --get cvsimport.module`);
110 push(@ARGV, $module);
114 my $opts = "haivmkuo:d:p:C:z:s:M:P:A:S:L:";
115 read_repo_config($opts);
116 getopts($opts) or usage();
119 @ARGV <= 1 or usage();
122 $ENV{"CVSROOT"} = $opt_d;
123 } elsif (-f 'CVS/Root') {
124 open my $f, '<', 'CVS/Root' or die 'Failed to open CVS/Root';
128 $ENV{"CVSROOT"} = $opt_d;
129 } elsif ($ENV{"CVSROOT"}) {
130 $opt_d = $ENV{"CVSROOT"};
132 die "CVSROOT needs to be set";
138 my $git_tree = $opt_C;
143 $cvs_tree = $ARGV[0];
144 } elsif (-f 'CVS/Repository') {
145 open my $f, '<', 'CVS/Repository' or
146 die 'Failed to open CVS/Repository';
156 @mergerx = ( qr/\W(?:from|of|merge|merging|merged) (\w+)/i );
159 push (@mergerx, qr/$opt_M/);
162 # Remember UTC of our starting time
163 # we'll want to avoid importing commits
164 # that are too recent
165 our $starttime = time();
167 select(STDERR); $|=1; select(STDOUT);
172 # We're only interested in connecting and downloading, so ...
175 use File::Temp qw(tempfile);
176 use POSIX qw(strftime dup2);
179 my ($what,$repo,$subdir) = @_;
180 $what=ref($what) if ref($what);
183 $self->{'buffer'} = "";
187 $self->{'fullrep'} = $repo;
190 $self->{'subdir'} = $subdir;
191 $self->{'lines'} = undef;
198 my $repo = $self->{'fullrep'};
199 if ($repo =~ s/^:pserver(?:([^:]*)):(?:(.*?)(?::(.*?))?@)?([^:\/]*)(?::(\d*))?//) {
200 my ($param,$user,$pass,$serv,$port) = ($1,$2,$3,$4,$5);
202 my ($proxyhost,$proxyport);
203 if ($param && ($param =~ m/proxy=([^;]+)/)) {
205 # Default proxyport, if not specified, is 8080.
207 if ($ENV{"CVS_PROXY_PORT"}) {
208 $proxyport = $ENV{"CVS_PROXY_PORT"};
210 if ($param =~ m/proxyport=([^;]+)/) {
215 $user="anonymous" unless defined $user;
218 $rr2 = ":pserver:$user\@$serv:$repo";
221 my $rr = ":pserver:$user\@$serv:$port$repo";
224 open(H,$ENV{'HOME'}."/.cvspass") and do {
225 # :pserver:cvs@mea.tmt.tele.fi:/cvsroot/zmailer Ah<Z
229 my ($w,$p) = split(/\s/,$_,2);
230 if ($w eq $rr or $w eq $rr2) {
237 $pass="A" unless $pass;
242 # Use a HTTP Proxy. Only works for HTTP proxies that
243 # don't require user authentication
245 # See: http://www.ietf.org/rfc/rfc2817.txt
247 $s = IO::Socket::INET->new(PeerHost => $proxyhost, PeerPort => $proxyport);
248 die "Socket to $proxyhost: $!\n" unless defined $s;
249 $s->write("CONNECT $serv:$port HTTP/1.1\r\nHost: $serv:$port\r\n\r\n")
250 or die "Write to $proxyhost: $!\n";
255 # The answer should look like 'HTTP/1.x 2yy ....'
256 if (!($rep =~ m#^HTTP/1\.. 2[0-9][0-9]#)) {
257 die "Proxy connect: $rep\n";
259 # Skip up to the empty line of the proxy server output
260 # including the response headers.
261 while ($rep = <$s>) {
262 last if (!defined $rep ||
267 $s = IO::Socket::INET->new(PeerHost => $serv, PeerPort => $port);
268 die "Socket to $serv: $!\n" unless defined $s;
271 $s->write("BEGIN AUTH REQUEST\n$repo\n$user\n$pass\nEND AUTH REQUEST\n")
272 or die "Write to $serv: $!\n";
277 if ($rep ne "I LOVE YOU\n") {
278 $rep="<unknown>" unless $rep;
279 die "AuthReply: $rep\n";
281 $self->{'socketo'} = $s;
282 $self->{'socketi'} = $s;
283 } else { # local or ext: Fork off our own cvs server.
284 my $pr = IO::Pipe->new();
285 my $pw = IO::Pipe->new();
287 die "Fork: $!\n" unless defined $pid;
289 $cvs = $ENV{CVS_SERVER} if exists $ENV{CVS_SERVER};
291 $rsh = $ENV{CVS_RSH} if exists $ENV{CVS_RSH};
293 my @cvs = ($cvs, 'server');
294 my ($local, $user, $host);
295 $local = $repo =~ s/:local://;
298 $local = !($repo =~ s/^(?:([^\@:]+)\@)?([^:]+)://);
299 ($user, $host) = ($1, $2);
303 unshift @cvs, $rsh, '-l', $user, $host;
305 unshift @cvs, $rsh, $host;
312 dup2($pw->fileno(),0);
313 dup2($pr->fileno(),1);
320 $self->{'socketo'} = $pw;
321 $self->{'socketi'} = $pr;
323 $self->{'socketo'}->write("Root $repo\n");
325 # Trial and error says that this probably is the minimum set
326 $self->{'socketo'}->write("Valid-responses ok error Valid-requests Mode M Mbinary E Checked-in Created Updated Merged Removed\n");
328 $self->{'socketo'}->write("valid-requests\n");
329 $self->{'socketo'}->flush();
331 chomp(my $rep=$self->readline());
332 if ($rep !~ s/^Valid-requests\s*//) {
333 $rep="<unknown>" unless $rep;
334 die "Expected Valid-requests from server, but got: $rep\n";
336 chomp(my $res=$self->readline());
337 die "validReply: $res\n" if $res ne "ok";
339 $self->{'socketo'}->write("UseUnchanged\n") if $rep =~ /\bUseUnchanged\b/;
340 $self->{'repo'} = $repo;
345 return $self->{'socketi'}->getline();
349 # Request a file with a given revision.
350 # Trial and error says this is a good way to do it. :-/
351 my ($self,$fn,$rev) = @_;
352 $self->{'socketo'}->write("Argument -N\n") or return undef;
353 $self->{'socketo'}->write("Argument -P\n") or return undef;
354 # -kk: Linus' version doesn't use it - defaults to off
356 $self->{'socketo'}->write("Argument -kk\n") or return undef;
358 $self->{'socketo'}->write("Argument -r\n") or return undef;
359 $self->{'socketo'}->write("Argument $rev\n") or return undef;
360 $self->{'socketo'}->write("Argument --\n") or return undef;
361 $self->{'socketo'}->write("Argument $self->{'subdir'}/$fn\n") or return undef;
362 $self->{'socketo'}->write("Directory .\n") or return undef;
363 $self->{'socketo'}->write("$self->{'repo'}\n") or return undef;
364 # $self->{'socketo'}->write("Sticky T1.0\n") or return undef;
365 $self->{'socketo'}->write("co\n") or return undef;
366 $self->{'socketo'}->flush() or return undef;
367 $self->{'lines'} = 0;
371 # Read a line from the server.
372 # ... except that 'line' may be an entire file. ;-)
373 my ($self, $fh) = @_;
374 die "Not in lines" unless defined $self->{'lines'};
378 while (defined($line = $self->readline())) {
379 # M U gnupg-cvs-rep/AUTHORS
380 # Updated gnupg-cvs-rep/
381 # /daten/src/rsync/gnupg-cvs-rep/AUTHORS
382 # /AUTHORS/1.1///T1.1
387 if ($line =~ s/^(?:Created|Updated) //) {
388 $line = $self->readline(); # path
389 $line = $self->readline(); # Entries line
390 my $mode = $self->readline(); chomp $mode;
391 $self->{'mode'} = $mode;
392 defined (my $cnt = $self->readline())
393 or die "EOF from server after 'Changed'\n";
395 die "Duh: Filesize $cnt" if $cnt !~ /^\d+$/;
397 $res = $self->_fetchfile($fh, $cnt);
398 } elsif ($line =~ s/^ //) {
400 $res += length($line);
401 } elsif ($line =~ /^M\b/) {
403 } elsif ($line =~ /^Mbinary\b/) {
405 die "EOF from server after 'Mbinary'" unless defined ($cnt = $self->readline());
407 die "Duh: Mbinary $cnt" if $cnt !~ /^\d+$/ or $cnt<1;
409 $res += $self->_fetchfile($fh, $cnt);
413 # print STDERR "S: ok (".length($res).")\n";
415 } elsif ($line =~ s/^E //) {
416 # print STDERR "S: $line\n";
417 } elsif ($line =~ /^(Remove-entry|Removed) /i) {
418 $line = $self->readline(); # filename
419 $line = $self->readline(); # OK
421 die "Unknown: $line" if $line ne "ok";
424 die "Unknown: $line\n";
431 my ($self,$fn,$rev) = @_;
434 my ($fh, $name) = tempfile('gitcvs.XXXXXX',
435 DIR => File::Spec->tmpdir(), UNLINK => 1);
437 $self->_file($fn,$rev) and $res = $self->_line($fh);
440 print STDERR "Server has gone away while fetching $fn $rev, retrying...\n";
443 $self->_file($fn,$rev) or die "No file command send";
444 $res = $self->_line($fh);
445 die "Retry failed" unless defined $res;
449 return ($name, $res);
452 my ($self, $fh, $cnt) = @_;
454 my $bufsize = 1024 * 1024;
456 if ($bufsize > $cnt) {
460 my $num = $self->{'socketi'}->read($buf,$bufsize);
461 die "Server: Filesize $cnt: $num: $!\n" if not defined $num or $num<=0;
472 my $cvs = CVSconn->new($opt_d, $cvs_tree);
477 m#(\d{2,4})/(\d\d)/(\d\d)\s(\d\d):(\d\d)(?::(\d\d))?#
478 or die "Unparseable date: $d\n";
479 my $y=$1; $y-=1900 if $y>1900;
480 return timegm($6||0,$5,$4,$3,$2-1,$y);
488 for my $x(split(//,$mode)) {
493 } elsif ($x eq "u") { $um |= 0700;
494 } elsif ($x eq "g") { $um |= 0070;
495 } elsif ($x eq "o") { $um |= 0007;
496 } elsif ($x eq "r") { $mm |= 0444;
497 } elsif ($x eq "w") { $mm |= 0222;
498 } elsif ($x eq "x") { $mm |= 0111;
499 } elsif ($x eq "=") { # do nothing
500 } else { die "Unknown mode: $mode\n";
515 return $s =~ /^[a-f0-9]{40}$/;
518 sub get_headref ($$) {
522 my $f = "$git_dir/refs/heads/$name";
523 if (open(my $fh, $f)) {
524 chomp(my $r = <$fh>);
525 is_sha1($r) or die "Cannot get head id for $name ($r): $!";
528 die "unable to open $f: $!" unless $! == POSIX::ENOENT;
533 or mkdir($git_tree,0777)
534 or die "Could not create $git_tree: $!";
537 my $last_branch = "";
538 my $orig_branch = "";
540 my $tip_at_start = undef;
542 my $git_dir = $ENV{"GIT_DIR"} || ".git";
543 $git_dir = getwd()."/".$git_dir unless $git_dir =~ m#^/#;
544 $ENV{"GIT_DIR"} = $git_dir;
546 $orig_git_index = $ENV{GIT_INDEX_FILE} if exists $ENV{GIT_INDEX_FILE};
548 my %index; # holds filenames of one index per branch
550 unless (-d $git_dir) {
552 die "Cannot init the GIT db at $git_tree: $?\n" if $?;
553 system("git-read-tree");
554 die "Cannot init an empty tree: $?\n" if $?;
556 $last_branch = $opt_o;
559 -f "$git_dir/refs/heads/$opt_o"
560 or die "Branch '$opt_o' does not exist.\n".
561 "Either use the correct '-o branch' option,\n".
562 "or import to a new repository.\n";
564 open(F, "git-symbolic-ref HEAD |") or
565 die "Cannot run git-symbolic-ref: $!\n";
566 chomp ($last_branch = <F>);
567 $last_branch = basename($last_branch);
569 unless ($last_branch) {
570 warn "Cannot read the last branch name: $! -- assuming 'master'\n";
571 $last_branch = "master";
573 $orig_branch = $last_branch;
574 $tip_at_start = `git-rev-parse --verify HEAD`;
576 # Get the last import timestamps
577 my $fmt = '($ref, $author) = (%(refname), %(author));';
578 open(H, "git-for-each-ref --perl --format='$fmt' refs/heads |") or
579 die "Cannot run git-for-each-ref: $!\n";
580 while (defined(my $entry = <H>)) {
582 eval($entry) || die "cannot eval refs list: $@";
583 my ($head) = ($ref =~ m|^refs/heads/(.*)|);
584 $author =~ /^.*\s(\d+)\s[-+]\d{4}$/;
585 $branch_date{$head} = $1;
591 or die "Could not create git subdir ($git_dir).\n";
593 # now we read (and possibly save) author-info as well
594 -f "$git_dir/cvs-authors" and
595 read_author_info("$git_dir/cvs-authors");
597 read_author_info($opt_A);
598 write_author_info("$git_dir/cvs-authors");
603 # run cvsps into a file unless we are getting
604 # it passed as a file via $opt_P
608 print "Running cvsps...\n" if $opt_v;
609 my $pid = open(CVSPS,"-|");
611 die "Cannot fork: $!\n" unless defined $pid;
614 @opt = split(/,/,$opt_p) if defined $opt_p;
615 unshift @opt, '-z', $opt_z if defined $opt_z;
616 unshift @opt, '-q' unless defined $opt_v;
617 unless (defined($opt_p) && $opt_p =~ m/--no-cvs-direct/) {
618 push @opt, '--cvs-direct';
620 exec("cvsps","--norc",@opt,"-u","-A",'--root',$opt_d,$cvs_tree);
621 die "Could not start cvsps: $!\n";
623 ($cvspsfh, $cvspsfile) = tempfile('gitXXXXXX', SUFFIX => '.cvsps',
624 DIR => File::Spec->tmpdir());
634 open(CVS, "<$cvspsfile") or die $!;
637 #---------------------
639 #Date: 1999/09/18 13:03:59
641 #Branch: STABLE-BRANCH-1-0
642 #Ancestor branch: HEAD
645 # See ChangeLog: Sat Sep 18 13:03:28 CEST 1999 Werner Koch
647 # README:1.57->1.57.2.1
648 # VERSION:1.96->1.96.2.1
650 #---------------------
654 sub update_index (\@\@) {
657 open(my $fh, '|-', qw(git-update-index -z --index-info))
658 or die "unable to open git-update-index: $!";
660 (map { "0 0000000000000000000000000000000000000000\t$_\0" }
662 (map { '100' . sprintf('%o', $_->[0]) . " $_->[1]\t$_->[2]\0" }
664 or die "unable to write to git-update-index: $!";
666 or die "unable to write to git-update-index: $!";
667 $? and die "git-update-index reported error: $?";
671 open(my $fh, '-|', qw(git-write-tree))
672 or die "unable to open git-write-tree: $!";
673 chomp(my $tree = <$fh>);
675 or die "Cannot get tree id ($tree): $!";
677 or die "Error running git-write-tree: $?\n";
678 print "Tree ID $tree\n" if $opt_v;
682 my ($patchset,$date,$author_name,$author_email,$branch,$ancestor,$tag,$logmsg);
683 my (@old,@new,@skipped,%ignorebranch);
685 # commits that cvsps cannot place anywhere...
686 $ignorebranch{'#CVSPS_NO_BRANCH'} = 1;
689 if ($branch eq $opt_o && !$index{branch} && !get_headref($branch, $git_dir)) {
690 # looks like an initial commit
691 # use the index primed by git-init
692 $ENV{GIT_INDEX_FILE} = '.git/index';
693 $index{$branch} = '.git/index';
695 # use an index per branch to speed up
696 # imports of projects with many branches
697 unless ($index{$branch}) {
698 $index{$branch} = tmpnam();
699 $ENV{GIT_INDEX_FILE} = $index{$branch};
701 system("git-read-tree", $ancestor);
703 system("git-read-tree", $branch);
705 die "read-tree failed: $?\n" if $?;
708 $ENV{GIT_INDEX_FILE} = $index{$branch};
710 update_index(@old, @new);
712 my $tree = write_tree();
713 my $parent = get_headref($last_branch, $git_dir);
714 print "Parent ID " . ($parent ? $parent : "(empty)") . "\n" if $opt_v;
717 push @commit_args, ("-p", $parent) if $parent;
719 # loose detection of merges
720 # based on the commit msg
721 foreach my $rx (@mergerx) {
722 next unless $logmsg =~ $rx && $1;
723 my $mparent = $1 eq 'HEAD' ? $opt_o : $1;
724 if (my $sha1 = get_headref($mparent, $git_dir)) {
725 push @commit_args, '-p', $mparent;
726 print "Merge parent branch: $mparent\n" if $opt_v;
730 my $commit_date = strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date));
731 $ENV{GIT_AUTHOR_NAME} = $author_name;
732 $ENV{GIT_AUTHOR_EMAIL} = $author_email;
733 $ENV{GIT_AUTHOR_DATE} = $commit_date;
734 $ENV{GIT_COMMITTER_NAME} = $author_name;
735 $ENV{GIT_COMMITTER_EMAIL} = $author_email;
736 $ENV{GIT_COMMITTER_DATE} = $commit_date;
737 my $pid = open2(my $commit_read, my $commit_write,
738 'git-commit-tree', $tree, @commit_args);
740 # compatibility with git2cvs
741 substr($logmsg,32767) = "" if length($logmsg) > 32767;
742 $logmsg =~ s/[\s\n]+\z//;
745 $logmsg .= "\n\n\nSKIPPED:\n\t";
746 $logmsg .= join("\n\t", @skipped) . "\n";
750 print($commit_write "$logmsg\n") && close($commit_write)
751 or die "Error writing to git-commit-tree: $!\n";
753 print "Committed patch $patchset ($branch $commit_date)\n" if $opt_v;
754 chomp(my $cid = <$commit_read>);
755 is_sha1($cid) or die "Cannot get commit id ($cid): $!\n";
756 print "Commit ID $cid\n" if $opt_v;
760 die "Error running git-commit-tree: $?\n" if $?;
762 system("git-update-ref refs/heads/$branch $cid") == 0
763 or die "Cannot write branch $branch for update: $!\n";
766 my ($in, $out) = ('','');
768 $xtag =~ s/\s+\*\*.*$//; # Remove stuff like ** INVALID ** and ** FUNKY **
769 $xtag =~ tr/_/\./ if ( $opt_u );
770 $xtag =~ s/[\/]/$opt_s/g;
772 my $pid = open2($in, $out, 'git-mktag');
773 print $out "object $cid\n".
776 "tagger $author_name <$author_email>\n"
777 or die "Cannot create tag object $xtag: $!\n";
779 or die "Cannot create tag object $xtag: $!\n";
784 if ( !close($in) or waitpid($pid, 0) != $pid or
785 $? != 0 or $tagobj !~ /^[0123456789abcdef]{40}$/ ) {
786 die "Cannot create tag object $xtag: $!\n";
790 open(C,">$git_dir/refs/tags/$xtag")
791 or die "Cannot create tag $xtag: $!\n";
793 or die "Cannot write tag $xtag: $!\n";
795 or die "Cannot write tag $xtag: $!\n";
797 print "Created tag '$xtag' on '$branch'\n" if $opt_v;
804 if ($state == 0 and /^-+$/) {
806 } elsif ($state == 0) {
809 } elsif (($state==0 or $state==1) and s/^PatchSet\s+//) {
812 } elsif ($state == 2 and s/^Date:\s+//) {
815 print STDERR "Could not parse date: $_\n";
820 } elsif ($state == 3 and s/^Author:\s+//) {
822 if (/^(.*?)\s+<(.*)>/) {
823 ($author_name, $author_email) = ($1, $2);
824 } elsif ($conv_author_name{$_}) {
825 $author_name = $conv_author_name{$_};
826 $author_email = $conv_author_email{$_};
828 $author_name = $author_email = $_;
831 } elsif ($state == 4 and s/^Branch:\s+//) {
836 } elsif ($state == 5 and s/^Ancestor branch:\s+//) {
839 $ancestor = $opt_o if $ancestor eq "HEAD";
841 } elsif ($state == 5) {
845 } elsif ($state == 6 and s/^Tag:\s+//) {
847 if ($_ eq "(none)") {
853 } elsif ($state == 7 and /^Log:/) {
856 } elsif ($state == 8 and /^Members:/) {
857 $branch = $opt_o if $branch eq "HEAD";
858 if (defined $branch_date{$branch} and $branch_date{$branch} >= $date) {
860 print "skip patchset $patchset: $date before $branch_date{$branch}\n" if $opt_v;
864 if (!$opt_a && $starttime - 300 - (defined $opt_z ? $opt_z : 300) <= $date) {
865 # skip if the commit is too recent
866 # that the cvsps default fuzz is 300s, we give ourselves another
867 # 300s just in case -- this also prevents skipping commits
868 # due to server clock drift
869 print "skip patchset $patchset: $date too recent\n" if $opt_v;
873 if (exists $ignorebranch{$branch}) {
874 print STDERR "Skipping $branch\n";
879 if ($ancestor eq $branch) {
880 print STDERR "Branch $branch erroneously stems from itself -- changed ancestor to $opt_o\n";
883 if (-f "$git_dir/refs/heads/$branch") {
884 print STDERR "Branch $branch already exists!\n";
888 unless (open(H,"$git_dir/refs/heads/$ancestor")) {
889 print STDERR "Branch $ancestor does not exist!\n";
890 $ignorebranch{$branch} = 1;
896 unless (open(H,"> $git_dir/refs/heads/$branch")) {
897 print STDERR "Could not create branch $branch: $!\n";
898 $ignorebranch{$branch} = 1;
903 or die "Could not write branch $branch: $!";
905 or die "Could not write branch $branch: $!";
907 $last_branch = $branch if $branch ne $last_branch;
909 } elsif ($state == 8) {
911 } elsif ($state == 9 and /^\s+(.+?):(INITIAL|\d+(?:\.\d+)+)->(\d+(?:\.\d+)+)\s*$/) {
912 # VERSION:1.96->1.96.2.1
913 my $init = ($2 eq "INITIAL");
917 if ($opt_S && $fn =~ m/$opt_S/) {
918 print "SKIPPING $fn v $rev\n";
922 print "Fetching $fn v $rev\n" if $opt_v;
923 my ($tmpname, $size) = $cvs->file($fn,$rev);
926 print "Drop $fn\n" if $opt_v;
928 print "".($init ? "New" : "Update")." $fn: $size bytes\n" if $opt_v;
929 my $pid = open(my $F, '-|');
930 die $! unless defined $pid;
932 exec("git-hash-object", "-w", $tmpname)
933 or die "Cannot create object: $!\n";
938 my $mode = pmode($cvs->{'mode'});
939 push(@new,[$mode, $sha, $fn]); # may be resurrected!
942 } elsif ($state == 9 and /^\s+(.+?):\d+(?:\.\d+)+->(\d+(?:\.\d+)+)\(DEAD\)\s*$/) {
946 print "Delete $fn\n" if $opt_v;
947 } elsif ($state == 9 and /^\s*$/) {
949 } elsif (($state == 9 or $state == 10) and /^-+$/) {
951 if ($opt_L && $commitcount > $opt_L) {
955 if (($commitcount & 1023) == 0) {
956 system("git repack -a -d");
959 } elsif ($state == 11 and /^-+$/) {
961 } elsif (/^-+$/) { # end of unknown-line processing
963 } elsif ($state != 11) { # ignore stuff when skipping
964 print "* UNKNOWN LINE * $_\n";
967 commit() if $branch and $state != 11;
973 # The heuristic of repacking every 1024 commits can leave a
974 # lot of unpacked data. If there is more than 1MB worth of
975 # not-packed objects, repack once more.
976 my $line = `git-count-objects`;
977 if ($line =~ /^(\d+) objects, (\d+) kilobytes$/) {
978 my ($n_objects, $kb) = ($1, $2);
980 and system("git repack -a -d");
983 foreach my $git_index (values %index) {
984 if ($git_index ne '.git/index') {
989 if (defined $orig_git_index) {
990 $ENV{GIT_INDEX_FILE} = $orig_git_index;
992 delete $ENV{GIT_INDEX_FILE};
995 # Now switch back to the branch we were in before all of this happened
997 print "DONE.\n" if $opt_v;
1001 my $tip_at_end = `git-rev-parse --verify HEAD`;
1002 if ($tip_at_start ne $tip_at_end) {
1003 for ($tip_at_start, $tip_at_end) { chomp; }
1004 print "Fetched into the current branch.\n" if $opt_v;
1005 system(qw(git-read-tree -u -m),
1006 $tip_at_start, $tip_at_end);
1007 die "Fast-forward update failed: $?\n" if $?;
1010 system(qw(git-merge cvsimport HEAD), "refs/heads/$opt_o");
1011 die "Could not merge $opt_o into the current branch.\n" if $?;
1014 $orig_branch = "master";
1015 print "DONE; creating $orig_branch branch\n" if $opt_v;
1016 system("git-update-ref", "refs/heads/master", "refs/heads/$opt_o")
1017 unless -f "$git_dir/refs/heads/master";
1018 system('git-update-ref', 'HEAD', "$orig_branch");
1020 system('git checkout');
1021 die "checkout failed: $?\n" if $?;