git-cvsimport-scripton commit Make specification of CVS module to convert optional. (f9714a4)
   1#!/usr/bin/perl -w
   2
   3# This tool is copyright (c) 2005, Matthias Urlichs.
   4# It is released under the Gnu Public License, version 2.
   5#
   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.
   9#
  10# Checking out the files is done by a single long-running CVS connection
  11# / server process.
  12#
  13# The head revision is on branch "origin" by default.
  14# You can change that with the '-o' option.
  15
  16use strict;
  17use warnings;
  18use Getopt::Std;
  19use File::Path qw(mkpath);
  20use File::Basename qw(basename dirname);
  21use Time::Local;
  22use IO::Socket;
  23use IO::Pipe;
  24use POSIX qw(strftime dup2);
  25
  26$SIG{'PIPE'}="IGNORE";
  27$ENV{'TZ'}="UTC";
  28
  29our($opt_h,$opt_o,$opt_v,$opt_d,$opt_p,$opt_C);
  30
  31sub usage() {
  32        print STDERR <<END;
  33Usage: ${\basename $0}     # fetch/update GIT from CVS
  34           [ -o branch-for-HEAD ] [ -h ] [ -v ] [ -d CVSROOT ]
  35       [ -p opts-for-cvsps ] [ -C GIT_repository ]
  36       [ CVS_module ]
  37END
  38        exit(1);
  39}
  40
  41getopts("hqvo:d:p:C:") or usage();
  42usage if $opt_h;
  43
  44@ARGV <= 1 or usage();
  45
  46if($opt_d) {
  47        $ENV{"CVSROOT"} = $opt_d;
  48} elsif(-f 'CVS/Root') {
  49        open my $f, '<', 'CVS/Root' or die 'Failed to open CVS/Root';
  50        $opt_d = <$f>;
  51        chomp $opt_d;
  52        close $f;
  53        $ENV{"CVSROOT"} = $opt_d;
  54} elsif($ENV{"CVSROOT"}) {
  55        $opt_d = $ENV{"CVSROOT"};
  56} else {
  57        die "CVSROOT needs to be set";
  58}
  59$opt_o ||= "origin";
  60my $git_tree = $opt_C;
  61$git_tree ||= ".";
  62
  63my $cvs_tree;
  64if ($#ARGV == 0) {
  65        $cvs_tree = $ARGV[0];
  66} elsif (-f 'CVS/Repository') {
  67        open my $f, '<', 'CVS/Repository' or 
  68            die 'Failed to open CVS/Repository';
  69        $cvs_tree = <$f>;
  70        chomp $cvs_tree;
  71        close $f
  72} else {
  73        usage();
  74}
  75
  76select(STDERR); $|=1; select(STDOUT);
  77
  78
  79package CVSconn;
  80# Basic CVS dialog.
  81# We're only interested in connecting and downloading, so ...
  82
  83use POSIX qw(strftime dup2);
  84
  85sub new {
  86        my($what,$repo,$subdir) = @_;
  87        $what=ref($what) if ref($what);
  88
  89        my $self = {};
  90        $self->{'buffer'} = "";
  91        bless($self,$what);
  92
  93        $repo =~ s#/+$##;
  94        $self->{'fullrep'} = $repo;
  95        $self->conn();
  96
  97        $self->{'subdir'} = $subdir;
  98        $self->{'lines'} = undef;
  99
 100        return $self;
 101}
 102
 103sub conn {
 104        my $self = shift;
 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;
 109                my $rr2 = "-";
 110                unless($port) {
 111                        $rr2 = ":pserver:$user\@$serv:$repo";
 112                        $port=2401;
 113                }
 114                my $rr = ":pserver:$user\@$serv:$port$repo";
 115
 116                unless($pass) {
 117                        open(H,$ENV{'HOME'}."/.cvspass") and do {
 118                                # :pserver:cvs@mea.tmt.tele.fi:/cvsroot/zmailer Ah<Z
 119                                while(<H>) {
 120                                        chomp;
 121                                        s/^\/\d+\s+//;
 122                                        my ($w,$p) = split(/\s/,$_,2);
 123                                        if($w eq $rr or $w eq $rr2) {
 124                                                $pass = $p;
 125                                                last;
 126                                        }
 127                                }
 128                        };
 129                }
 130                $pass="A" unless $pass;
 131
 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";
 136                $s->flush();
 137
 138                my $rep = <$s>;
 139
 140                if($rep ne "I LOVE YOU\n") {
 141                        $rep="<unknown>" unless $rep;
 142                        die "AuthReply: $rep\n";
 143                }
 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();
 149                my $pid = fork();
 150                die "Fork: $!\n" unless defined $pid;
 151                unless($pid) {
 152                        $pr->writer();
 153                        $pw->reader();
 154                        dup2($pw->fileno(),0);
 155                        dup2($pr->fileno(),1);
 156                        $pr->close();
 157                        $pw->close();
 158                        exec("cvs","server");
 159                }
 160                $pw->writer();
 161                $pr->reader();
 162                $self->{'socketo'} = $pw;
 163                $self->{'socketi'} = $pr;
 164        }
 165        $self->{'socketo'}->write("Root $repo\n");
 166
 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");
 169
 170        $self->{'socketo'}->write("valid-requests\n");
 171        $self->{'socketo'}->flush();
 172
 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";
 177        }
 178        chomp(my $res=$self->readline());
 179        die "validReply: $res\n" if $res ne "ok";
 180
 181        $self->{'socketo'}->write("UseUnchanged\n") if $rep =~ /\bUseUnchanged\b/;
 182        $self->{'repo'} = $repo;
 183}
 184
 185sub readline {
 186        my($self) = @_;
 187        return $self->{'socketi'}->getline();
 188}
 189
 190sub _file {
 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;
 208        return 1;
 209}
 210sub _line {
 211        # Read a line from the server.
 212        # ... except that 'line' may be an entire file. ;-)
 213        my($self) = @_;
 214        die "Not in lines" unless defined $self->{'lines'};
 215
 216        my $line;
 217        my $res="";
 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
 223                # u=rw,g=rw,o=rw
 224                # 0
 225                # ok
 226
 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";
 234                        chomp $cnt;
 235                        die "Duh: Filesize $cnt" if $cnt !~ /^\d+$/;
 236                        $line="";
 237                        $res="";
 238                        while($cnt) {
 239                                my $buf;
 240                                my $num = $self->{'socketi'}->read($buf,$cnt);
 241                                die "Server: Filesize $cnt: $num: $!\n" if not defined $num or $num<=0;
 242                                $res .= $buf;
 243                                $cnt -= $num;
 244                        }
 245                } elsif($line =~ s/^ //) {
 246                        $res .= $line;
 247                } elsif($line =~ /^M\b/) {
 248                        # output, do nothing
 249                } elsif($line =~ /^Mbinary\b/) {
 250                        my $cnt;
 251                        die "EOF from server after 'Mbinary'" unless defined ($cnt = $self->readline());
 252                        chomp $cnt;
 253                        die "Duh: Mbinary $cnt" if $cnt !~ /^\d+$/ or $cnt<1;
 254                        $line="";
 255                        while($cnt) {
 256                                my $buf;
 257                                my $num = $self->{'socketi'}->read($buf,$cnt);
 258                                die "S: Mbinary $cnt: $num: $!\n" if not defined $num or $num<=0;
 259                                $res .= $buf;
 260                                $cnt -= $num;
 261                        }
 262                } else {
 263                        chomp $line;
 264                        if($line eq "ok") {
 265                                # print STDERR "S: ok (".length($res).")\n";
 266                                return $res;
 267                        } elsif($line =~ s/^E //) {
 268                                # print STDERR "S: $line\n";
 269                        } else {
 270                                die "Unknown: $line\n";
 271                        }
 272                }
 273        }
 274}
 275sub file {
 276        my($self,$fn,$rev) = @_;
 277        my $res;
 278
 279        if ($self->_file($fn,$rev)) {
 280                $res = $self->_line();
 281                return $res if defined $res;
 282        }
 283
 284        # retry
 285        $self->conn();
 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;
 290        return $res;
 291}
 292
 293
 294package main;
 295
 296my $cvs = CVSconn->new($opt_d, $cvs_tree);
 297
 298
 299sub pdate($) {
 300        my($d) = @_;
 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);
 305}
 306
 307sub pmode($) {
 308        my($mode) = @_;
 309        my $m = 0;
 310        my $mm = 0;
 311        my $um = 0;
 312        for my $x(split(//,$mode)) {
 313                if($x eq ",") {
 314                        $m |= $mm&$um;
 315                        $mm = 0;
 316                        $um = 0;
 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";
 325                }
 326        }
 327        $m |= $mm&$um;
 328        return $m;
 329}
 330
 331my $tmpcv = "/var/cache/cvs";
 332
 333sub getwd() {
 334        my $pwd = `pwd`;
 335        chomp $pwd;
 336        return $pwd;
 337}
 338
 339-d $git_tree
 340        or mkdir($git_tree,0777)
 341        or die "Could not create $git_tree: $!";
 342chdir($git_tree);
 343
 344my $last_branch = "";
 345my $orig_branch = "";
 346my %branch_date;
 347
 348my $git_dir = $ENV{"GIT_DIR"} || ".git";
 349$git_dir = getwd()."/".$git_dir unless $git_dir =~ m#^/#;
 350$ENV{"GIT_DIR"} = $git_dir;
 351unless(-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 $?;
 356
 357        $last_branch = $opt_o;
 358        $orig_branch = "";
 359} else {
 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";
 364
 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";
 369        }
 370        $orig_branch = $last_branch;
 371
 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>);
 379                close(F);
 380                open(F,"git-cat-file commit $ftag |");
 381                while(<F>) {
 382                        next unless /^author\s.*\s(\d+)\s[-+]\d{4}$/;
 383                        $branch_date{$head} = $1;
 384                        last;
 385                }
 386                close(F);
 387        }
 388        closedir(D);
 389}
 390
 391-d $git_dir
 392        or die "Could not create git subdir ($git_dir).\n";
 393
 394my $pid = open(CVS,"-|");
 395die "Cannot fork: $!\n" unless defined $pid;
 396unless($pid) {
 397        my @opt;
 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";
 401}
 402
 403
 404## cvsps output:
 405#---------------------
 406#PatchSet 314
 407#Date: 1999/09/18 13:03:59
 408#Author: wkoch
 409#Branch: STABLE-BRANCH-1-0
 410#Ancestor branch: HEAD
 411#Tag: (none)
 412#Log:
 413#    See ChangeLog: Sat Sep 18 13:03:28 CEST 1999  Werner Koch
 414#Members:
 415#       README:1.57->1.57.2.1
 416#       VERSION:1.96->1.96.2.1
 417#
 418#---------------------
 419
 420my $state = 0;
 421
 422my($patchset,$date,$author,$branch,$ancestor,$tag,$logmsg);
 423my(@old,@new);
 424my $commit = sub {
 425        my $pid;
 426        while(@old) {
 427                my @o2;
 428                if(@old > 55) {
 429                        @o2 = splice(@old,0,50);
 430                } else {
 431                        @o2 = @old;
 432                        @old = ();
 433                }
 434                system("git-update-cache","--force-remove","--",@o2);
 435                die "Cannot remove files: $?\n" if $?;
 436        }
 437        while(@new) {
 438                my @n2;
 439                if(@new > 55) {
 440                        @n2 = splice(@new,0,50);
 441                } else {
 442                        @n2 = @new;
 443                        @new = ();
 444                }
 445                system("git-update-cache","--add","--",@n2);
 446                die "Cannot add files: $?\n" if $?;
 447        }
 448
 449        $pid = open(C,"-|");
 450        die "Cannot fork: $!" unless defined $pid;
 451        unless($pid) {
 452                exec("git-write-tree");
 453                die "Cannot exec git-write-tree: $!\n";
 454        }
 455        chomp(my $tree = <C>);
 456        length($tree) == 40
 457                or die "Cannot get tree id ($tree): $!\n";
 458        close(C)
 459                or die "Error running git-write-tree: $?\n";
 460        print "Tree ID $tree\n" if $opt_v;
 461
 462        my $parent = "";
 463        if(open(C,"$git_dir/refs/heads/$last_branch")) {
 464                chomp($parent = <C>);
 465                close(C);
 466                length($parent) == 40
 467                        or die "Cannot get parent id ($parent): $!\n";
 468                print "Parent ID $parent\n" if $opt_v;
 469        }
 470
 471        my $pr = IO::Pipe->new();
 472        my $pw = IO::Pipe->new();
 473        $pid = fork();
 474        die "Fork: $!\n" unless defined $pid;
 475        unless($pid) {
 476                $pr->writer();
 477                $pw->reader();
 478                dup2($pw->fileno(),0);
 479                dup2($pr->fileno(),1);
 480                $pr->close();
 481                $pw->close();
 482
 483                my @par = ();
 484                @par = ("-p",$parent) if $parent;
 485                exec("env",
 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";
 494        }
 495        $pw->writer();
 496        $pr->reader();
 497
 498        # compatibility with git2cvs
 499        substr($logmsg,32767) = "" if length($logmsg) > 32767;
 500        $logmsg =~ s/[\s\n]+\z//;
 501
 502        print $pw "$logmsg\n"
 503                or die "Error writing to git-commit-tree: $!\n";
 504        $pw->close();
 505
 506        print "Committed patch $patchset ($branch)\n" if $opt_v;
 507        chomp(my $cid = <$pr>);
 508        length($cid) == 40
 509                or die "Cannot get commit id ($cid): $!\n";
 510        print "Commit ID $cid\n" if $opt_v;
 511        $pr->close();
 512
 513        waitpid($pid,0);
 514        die "Error running git-commit-tree: $?\n" if $?;
 515
 516        open(C,">$git_dir/refs/heads/$branch")
 517                or die "Cannot open branch $branch for update: $!\n";
 518        print C "$cid\n"
 519                or die "Cannot write branch $branch for update: $!\n";
 520        close(C)
 521                or die "Cannot write branch $branch for update: $!\n";
 522
 523        if($tag) {
 524                open(C,">$git_dir/refs/tags/$tag")
 525                        or die "Cannot create tag $tag: $!\n";
 526                print C "$cid\n"
 527                        or die "Cannot write tag $branch: $!\n";
 528                close(C)
 529                        or die "Cannot write tag $branch: $!\n";
 530                print "Created tag '$tag' on '$branch'\n" if $opt_v;
 531        }
 532};
 533
 534while(<CVS>) {
 535        chomp;
 536        if($state == 0 and /^-+$/) {
 537                $state = 1;
 538        } elsif($state == 0) {
 539                $state = 1;
 540                redo;
 541        } elsif(($state==0 or $state==1) and s/^PatchSet\s+//) {
 542                $patchset = 0+$_;
 543                $state=2;
 544        } elsif($state == 2 and s/^Date:\s+//) {
 545                $date = pdate($_);
 546                unless($date) {
 547                        print STDERR "Could not parse date: $_\n";
 548                        $state=0;
 549                        next;
 550                }
 551                $state=3;
 552        } elsif($state == 3 and s/^Author:\s+//) {
 553                s/\s+$//;
 554                $author = $_;
 555                $state = 4;
 556        } elsif($state == 4 and s/^Branch:\s+//) {
 557                s/\s+$//;
 558                $branch = $_;
 559                $state = 5;
 560        } elsif($state == 5 and s/^Ancestor branch:\s+//) {
 561                s/\s+$//;
 562                $ancestor = $_;
 563                $ancestor = $opt_o if $ancestor eq "HEAD";
 564                $state = 6;
 565        } elsif($state == 5) {
 566                $ancestor = undef;
 567                $state = 6;
 568                redo;
 569        } elsif($state == 6 and s/^Tag:\s+//) {
 570                s/\s+$//;
 571                if($_ eq "(none)") {
 572                        $tag = undef;
 573                } else {
 574                        $tag = $_;
 575                }
 576                $state = 7;
 577        } elsif($state == 7 and /^Log:/) {
 578                $logmsg = "";
 579                $state = 8;
 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) {
 583                        # skip
 584                        print "skip patchset $patchset: $date before $branch_date{$branch}\n";
 585                        $state = 11;
 586                        next;
 587                }
 588                if($ancestor) {
 589                        if(-f "$git_dir/refs/heads/$branch") {
 590                                print STDERR "Branch $branch already exists!\n";
 591                                $state=11;
 592                                next;
 593                        }
 594                        unless(open(H,"$git_dir/refs/heads/$ancestor")) {
 595                                print STDERR "Branch $ancestor does not exist!\n";
 596                                $state=11;
 597                                next;
 598                        }
 599                        chomp(my $id = <H>);
 600                        close(H);
 601                        unless(open(H,"> $git_dir/refs/heads/$branch")) {
 602                                print STDERR "Could not create branch $branch: $!\n";
 603                                $state=11;
 604                                next;
 605                        }
 606                        print H "$id\n"
 607                                or die "Could not write branch $branch: $!";
 608                        close(H)
 609                                or die "Could not write branch $branch: $!";
 610                }
 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 $?;
 615                }
 616                if($branch ne $last_branch) {
 617                        unlink("$git_dir/HEAD");
 618                        symlink("refs/heads/$branch","$git_dir/HEAD");
 619                        $last_branch = $branch;
 620                }
 621                $state = 9;
 622        } elsif($state == 8) {
 623                $logmsg .= "$_\n";
 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");
 627                my $fn = $1;
 628                my $rev = $3;
 629                $fn =~ s#^/+##;
 630                my $data = $cvs->file($fn,$rev);
 631                print "".($init ? "New" : "Update")." $fn: ".length($data)." bytes.\n";
 632                mkpath(dirname($fn),$opt_v);
 633                open(F,"> ./$fn")
 634                        or die "Cannot create '$fn': $!\n";
 635                print F $data
 636                        or die "Cannot write to '$fn': $!\n";
 637                close(F)
 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*$/) {
 642                my $fn = $1;
 643                $fn =~ s#^/+##;
 644                push(@old,$fn);
 645        } elsif($state == 9 and /^\s*$/) {
 646                $state = 10;
 647        } elsif(($state == 9 or $state == 10) and /^-+$/) {
 648                &$commit();
 649                $state = 1;
 650        } elsif($state == 11 and /^-+$/) {
 651                $state = 1;
 652        } elsif(/^-+$/) { # end of unknown-line processing
 653                $state = 1;
 654        } elsif($state != 11) { # ignore stuff when skipping
 655                print "* UNKNOWN LINE * $_\n";
 656        }
 657}
 658&$commit() if $branch and $state != 11;
 659
 660# Now switch back to the branch we were in before all of this happened
 661if($orig_branch) {
 662        print "DONE; switching back to $orig_branch\n" if $opt_v;
 663} else {
 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";
 668}
 669
 670system("git-read-tree","-m","-u","$last_branch","$orig_branch");
 671die "read-tree failed: $?\n" if $?;
 672
 673unlink("$git_dir/HEAD");
 674symlink("refs/heads/$orig_branch","$git_dir/HEAD");
 675