Support :ext: access method.
[git.git] / git-cvsimport-script
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
16 use strict;
17 use warnings;
18 use Getopt::Std;
19 use File::Path qw(mkpath);
20 use File::Basename qw(basename dirname);
21 use Time::Local;
22 use IO::Socket;
23 use IO::Pipe;
24 use POSIX qw(strftime dup2);
25
26 $SIG{'PIPE'}="IGNORE";
27 $ENV{'TZ'}="UTC";
28
29 our($opt_h,$opt_o,$opt_v,$opt_d,$opt_p,$opt_C);
30
31 sub usage() {
32         print STDERR <<END;
33 Usage: ${\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 ]
37 END
38         exit(1);
39 }
40
41 getopts("hqvo:d:p:C:") or usage();
42 usage if $opt_h;
43
44 @ARGV <= 1 or usage();
45
46 if($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";
60 my $git_tree = $opt_C;
61 $git_tree ||= ".";
62
63 my $cvs_tree;
64 if ($#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
76 select(STDERR); $|=1; select(STDOUT);
77
78
79 package CVSconn;
80 # Basic CVS dialog.
81 # We're only interested in connecting and downloading, so ...
82
83 use POSIX qw(strftime dup2);
84
85 sub 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
103 sub 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 or ext: 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                 my $cvs = 'cvs';
152                 $cvs = $ENV{CVS_SERVER} if exists $ENV{CVS_SERVER};
153                 my $rsh = 'rsh';
154                 $rsh = $ENV{CVS_RSH} if exists $ENV{CVS_RSH};
155
156                 my @cvs = ($cvs, 'server');
157                 my ($local, $user, $host);
158                 $local = $repo =~ s/:local://;
159                 if (!$local) {
160                     $repo =~ s/:ext://;
161                     $local = !($repo =~ s/^(?:([^\@:]+)\@)?([^:]+)://);
162                     ($user, $host) = ($1, $2);
163                 }
164                 if (!$local) {
165                     if ($user) {
166                         unshift @cvs, $rsh, '-l', $user, $host;
167                     } else {
168                         unshift @cvs, $rsh, $host;
169                     }
170                 }
171
172                 unless($pid) {
173                         $pr->writer();
174                         $pw->reader();
175                         dup2($pw->fileno(),0);
176                         dup2($pr->fileno(),1);
177                         $pr->close();
178                         $pw->close();
179                         exec(@cvs);
180                 }
181                 $pw->writer();
182                 $pr->reader();
183                 $self->{'socketo'} = $pw;
184                 $self->{'socketi'} = $pr;
185         }
186         $self->{'socketo'}->write("Root $repo\n");
187
188         # Trial and error says that this probably is the minimum set
189         $self->{'socketo'}->write("Valid-responses ok error Valid-requests Mode M Mbinary E F Checked-in Created Updated Merged Removed\n");
190
191         $self->{'socketo'}->write("valid-requests\n");
192         $self->{'socketo'}->flush();
193
194         chomp(my $rep=$self->readline());
195         if($rep !~ s/^Valid-requests\s*//) {
196                 $rep="<unknown>" unless $rep;
197                 die "Expected Valid-requests from server, but got: $rep\n";
198         }
199         chomp(my $res=$self->readline());
200         die "validReply: $res\n" if $res ne "ok";
201
202         $self->{'socketo'}->write("UseUnchanged\n") if $rep =~ /\bUseUnchanged\b/;
203         $self->{'repo'} = $repo;
204 }
205
206 sub readline {
207         my($self) = @_;
208         return $self->{'socketi'}->getline();
209 }
210
211 sub _file {
212         # Request a file with a given revision.
213         # Trial and error says this is a good way to do it. :-/
214         my($self,$fn,$rev) = @_;
215         $self->{'socketo'}->write("Argument -N\n") or return undef;
216         $self->{'socketo'}->write("Argument -P\n") or return undef;
217         # $self->{'socketo'}->write("Argument -ko\n") or return undef;
218         # -ko: Linus' version doesn't use it
219         $self->{'socketo'}->write("Argument -r\n") or return undef;
220         $self->{'socketo'}->write("Argument $rev\n") or return undef;
221         $self->{'socketo'}->write("Argument --\n") or return undef;
222         $self->{'socketo'}->write("Argument $self->{'subdir'}/$fn\n") or return undef;
223         $self->{'socketo'}->write("Directory .\n") or return undef;
224         $self->{'socketo'}->write("$self->{'repo'}\n") or return undef;
225         # $self->{'socketo'}->write("Sticky T1.0\n") or return undef;
226         $self->{'socketo'}->write("co\n") or return undef;
227         $self->{'socketo'}->flush() or return undef;
228         $self->{'lines'} = 0;
229         return 1;
230 }
231 sub _line {
232         # Read a line from the server.
233         # ... except that 'line' may be an entire file. ;-)
234         my($self) = @_;
235         die "Not in lines" unless defined $self->{'lines'};
236
237         my $line;
238         my $res="";
239         while(defined($line = $self->readline())) {
240                 # M U gnupg-cvs-rep/AUTHORS
241                 # Updated gnupg-cvs-rep/
242                 # /daten/src/rsync/gnupg-cvs-rep/AUTHORS
243                 # /AUTHORS/1.1///T1.1
244                 # u=rw,g=rw,o=rw
245                 # 0
246                 # ok
247
248                 if($line =~ s/^(?:Created|Updated) //) {
249                         $line = $self->readline(); # path
250                         $line = $self->readline(); # Entries line
251                         my $mode = $self->readline(); chomp $mode;
252                         $self->{'mode'} = $mode;
253                         defined (my $cnt = $self->readline())
254                                 or die "EOF from server after 'Changed'\n";
255                         chomp $cnt;
256                         die "Duh: Filesize $cnt" if $cnt !~ /^\d+$/;
257                         $line="";
258                         $res="";
259                         while($cnt) {
260                                 my $buf;
261                                 my $num = $self->{'socketi'}->read($buf,$cnt);
262                                 die "Server: Filesize $cnt: $num: $!\n" if not defined $num or $num<=0;
263                                 $res .= $buf;
264                                 $cnt -= $num;
265                         }
266                 } elsif($line =~ s/^ //) {
267                         $res .= $line;
268                 } elsif($line =~ /^M\b/) {
269                         # output, do nothing
270                 } elsif($line =~ /^Mbinary\b/) {
271                         my $cnt;
272                         die "EOF from server after 'Mbinary'" unless defined ($cnt = $self->readline());
273                         chomp $cnt;
274                         die "Duh: Mbinary $cnt" if $cnt !~ /^\d+$/ or $cnt<1;
275                         $line="";
276                         while($cnt) {
277                                 my $buf;
278                                 my $num = $self->{'socketi'}->read($buf,$cnt);
279                                 die "S: Mbinary $cnt: $num: $!\n" if not defined $num or $num<=0;
280                                 $res .= $buf;
281                                 $cnt -= $num;
282                         }
283                 } else {
284                         chomp $line;
285                         if($line eq "ok") {
286                                 # print STDERR "S: ok (".length($res).")\n";
287                                 return $res;
288                         } elsif($line =~ s/^E //) {
289                                 # print STDERR "S: $line\n";
290                         } else {
291                                 die "Unknown: $line\n";
292                         }
293                 }
294         }
295 }
296 sub file {
297         my($self,$fn,$rev) = @_;
298         my $res;
299
300         if ($self->_file($fn,$rev)) {
301                 $res = $self->_line();
302                 return $res if defined $res;
303         }
304
305         # retry
306         $self->conn();
307         $self->_file($fn,$rev)
308                 or die "No file command send\n";
309         $res = $self->_line();
310         die "No input: $fn $rev\n" unless defined $res;
311         return $res;
312 }
313
314
315 package main;
316
317 my $cvs = CVSconn->new($opt_d, $cvs_tree);
318
319
320 sub pdate($) {
321         my($d) = @_;
322         m#(\d{2,4})/(\d\d)/(\d\d)\s(\d\d):(\d\d)(?::(\d\d))?#
323                 or die "Unparseable date: $d\n";
324         my $y=$1; $y-=1900 if $y>1900;
325         return timegm($6||0,$5,$4,$3,$2-1,$y);
326 }
327
328 sub pmode($) {
329         my($mode) = @_;
330         my $m = 0;
331         my $mm = 0;
332         my $um = 0;
333         for my $x(split(//,$mode)) {
334                 if($x eq ",") {
335                         $m |= $mm&$um;
336                         $mm = 0;
337                         $um = 0;
338                 } elsif($x eq "u") { $um |= 0700;
339                 } elsif($x eq "g") { $um |= 0070;
340                 } elsif($x eq "o") { $um |= 0007;
341                 } elsif($x eq "r") { $mm |= 0444;
342                 } elsif($x eq "w") { $mm |= 0222;
343                 } elsif($x eq "x") { $mm |= 0111;
344                 } elsif($x eq "=") { # do nothing
345                 } else { die "Unknown mode: $mode\n";
346                 }
347         }
348         $m |= $mm&$um;
349         return $m;
350 }
351
352 my $tmpcv = "/var/cache/cvs";
353
354 sub getwd() {
355         my $pwd = `pwd`;
356         chomp $pwd;
357         return $pwd;
358 }
359
360 -d $git_tree
361         or mkdir($git_tree,0777)
362         or die "Could not create $git_tree: $!";
363 chdir($git_tree);
364
365 my $last_branch = "";
366 my $orig_branch = "";
367 my %branch_date;
368
369 my $git_dir = $ENV{"GIT_DIR"} || ".git";
370 $git_dir = getwd()."/".$git_dir unless $git_dir =~ m#^/#;
371 $ENV{"GIT_DIR"} = $git_dir;
372 unless(-d $git_dir) {
373         system("git-init-db");
374         die "Cannot init the GIT db at $git_tree: $?\n" if $?;
375         system("git-read-tree");
376         die "Cannot init an empty tree: $?\n" if $?;
377
378         $last_branch = $opt_o;
379         $orig_branch = "";
380 } else {
381         -f "$git_dir/refs/head/$opt_o"
382                 or die "Branch '$opt_o' does not exist.\n".
383                        "Either use the correct '-o branch' option,\n".
384                        "or import to a new repository.\n";
385
386         $last_branch = basename(readlink("$git_dir/HEAD"));
387         unless($last_branch) {
388                 warn "Cannot read the last branch name: $! -- assuming 'master'\n";
389                 $last_branch = "master";
390         }
391         $orig_branch = $last_branch;
392
393         # Get the last import timestamps
394         opendir(D,"$git_dir/refs/heads");
395         while(defined(my $head = readdir(D))) {
396                 next if $head =~ /^\./;
397                 open(F,"$git_dir/refs/heads/$head")
398                         or die "Bad head branch: $head: $!\n";
399                 chomp(my $ftag = <F>);
400                 close(F);
401                 open(F,"git-cat-file commit $ftag |");
402                 while(<F>) {
403                         next unless /^author\s.*\s(\d+)\s[-+]\d{4}$/;
404                         $branch_date{$head} = $1;
405                         last;
406                 }
407                 close(F);
408         }
409         closedir(D);
410 }
411
412 -d $git_dir
413         or die "Could not create git subdir ($git_dir).\n";
414
415 my $pid = open(CVS,"-|");
416 die "Cannot fork: $!\n" unless defined $pid;
417 unless($pid) {
418         my @opt;
419         @opt = split(/,/,$opt_p) if defined $opt_p;
420         exec("cvsps",@opt,"-x","-A","--cvs-direct",'--root',$opt_d,$cvs_tree);
421         die "Could not start cvsps: $!\n";
422 }
423
424
425 ## cvsps output:
426 #---------------------
427 #PatchSet 314
428 #Date: 1999/09/18 13:03:59
429 #Author: wkoch
430 #Branch: STABLE-BRANCH-1-0
431 #Ancestor branch: HEAD
432 #Tag: (none)
433 #Log:
434 #    See ChangeLog: Sat Sep 18 13:03:28 CEST 1999  Werner Koch
435 #Members:
436 #       README:1.57->1.57.2.1
437 #       VERSION:1.96->1.96.2.1
438 #
439 #---------------------
440
441 my $state = 0;
442
443 my($patchset,$date,$author,$branch,$ancestor,$tag,$logmsg);
444 my(@old,@new);
445 my $commit = sub {
446         my $pid;
447         while(@old) {
448                 my @o2;
449                 if(@old > 55) {
450                         @o2 = splice(@old,0,50);
451                 } else {
452                         @o2 = @old;
453                         @old = ();
454                 }
455                 system("git-update-cache","--force-remove","--",@o2);
456                 die "Cannot remove files: $?\n" if $?;
457         }
458         while(@new) {
459                 my @n2;
460                 if(@new > 55) {
461                         @n2 = splice(@new,0,50);
462                 } else {
463                         @n2 = @new;
464                         @new = ();
465                 }
466                 system("git-update-cache","--add","--",@n2);
467                 die "Cannot add files: $?\n" if $?;
468         }
469
470         $pid = open(C,"-|");
471         die "Cannot fork: $!" unless defined $pid;
472         unless($pid) {
473                 exec("git-write-tree");
474                 die "Cannot exec git-write-tree: $!\n";
475         }
476         chomp(my $tree = <C>);
477         length($tree) == 40
478                 or die "Cannot get tree id ($tree): $!\n";
479         close(C)
480                 or die "Error running git-write-tree: $?\n";
481         print "Tree ID $tree\n" if $opt_v;
482
483         my $parent = "";
484         if(open(C,"$git_dir/refs/heads/$last_branch")) {
485                 chomp($parent = <C>);
486                 close(C);
487                 length($parent) == 40
488                         or die "Cannot get parent id ($parent): $!\n";
489                 print "Parent ID $parent\n" if $opt_v;
490         }
491
492         my $pr = IO::Pipe->new();
493         my $pw = IO::Pipe->new();
494         $pid = fork();
495         die "Fork: $!\n" unless defined $pid;
496         unless($pid) {
497                 $pr->writer();
498                 $pw->reader();
499                 dup2($pw->fileno(),0);
500                 dup2($pr->fileno(),1);
501                 $pr->close();
502                 $pw->close();
503
504                 my @par = ();
505                 @par = ("-p",$parent) if $parent;
506                 exec("env",
507                         "GIT_AUTHOR_NAME=$author",
508                         "GIT_AUTHOR_EMAIL=$author",
509                         "GIT_AUTHOR_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
510                         "GIT_COMMITTER_NAME=$author",
511                         "GIT_COMMITTER_EMAIL=$author",
512                         "GIT_COMMITTER_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
513                         "git-commit-tree", $tree,@par);
514                 die "Cannot exec git-commit-tree: $!\n";
515         }
516         $pw->writer();
517         $pr->reader();
518
519         # compatibility with git2cvs
520         substr($logmsg,32767) = "" if length($logmsg) > 32767;
521         $logmsg =~ s/[\s\n]+\z//;
522
523         print $pw "$logmsg\n"
524                 or die "Error writing to git-commit-tree: $!\n";
525         $pw->close();
526
527         print "Committed patch $patchset ($branch)\n" if $opt_v;
528         chomp(my $cid = <$pr>);
529         length($cid) == 40
530                 or die "Cannot get commit id ($cid): $!\n";
531         print "Commit ID $cid\n" if $opt_v;
532         $pr->close();
533
534         waitpid($pid,0);
535         die "Error running git-commit-tree: $?\n" if $?;
536
537         open(C,">$git_dir/refs/heads/$branch")
538                 or die "Cannot open branch $branch for update: $!\n";
539         print C "$cid\n"
540                 or die "Cannot write branch $branch for update: $!\n";
541         close(C)
542                 or die "Cannot write branch $branch for update: $!\n";
543
544         if($tag) {
545                 open(C,">$git_dir/refs/tags/$tag")
546                         or die "Cannot create tag $tag: $!\n";
547                 print C "$cid\n"
548                         or die "Cannot write tag $branch: $!\n";
549                 close(C)
550                         or die "Cannot write tag $branch: $!\n";
551                 print "Created tag '$tag' on '$branch'\n" if $opt_v;
552         }
553 };
554
555 while(<CVS>) {
556         chomp;
557         if($state == 0 and /^-+$/) {
558                 $state = 1;
559         } elsif($state == 0) {
560                 $state = 1;
561                 redo;
562         } elsif(($state==0 or $state==1) and s/^PatchSet\s+//) {
563                 $patchset = 0+$_;
564                 $state=2;
565         } elsif($state == 2 and s/^Date:\s+//) {
566                 $date = pdate($_);
567                 unless($date) {
568                         print STDERR "Could not parse date: $_\n";
569                         $state=0;
570                         next;
571                 }
572                 $state=3;
573         } elsif($state == 3 and s/^Author:\s+//) {
574                 s/\s+$//;
575                 $author = $_;
576                 $state = 4;
577         } elsif($state == 4 and s/^Branch:\s+//) {
578                 s/\s+$//;
579                 $branch = $_;
580                 $state = 5;
581         } elsif($state == 5 and s/^Ancestor branch:\s+//) {
582                 s/\s+$//;
583                 $ancestor = $_;
584                 $ancestor = $opt_o if $ancestor eq "HEAD";
585                 $state = 6;
586         } elsif($state == 5) {
587                 $ancestor = undef;
588                 $state = 6;
589                 redo;
590         } elsif($state == 6 and s/^Tag:\s+//) {
591                 s/\s+$//;
592                 if($_ eq "(none)") {
593                         $tag = undef;
594                 } else {
595                         $tag = $_;
596                 }
597                 $state = 7;
598         } elsif($state == 7 and /^Log:/) {
599                 $logmsg = "";
600                 $state = 8;
601         } elsif($state == 8 and /^Members:/) {
602                 $branch = $opt_o if $branch eq "HEAD";
603                 if(defined $branch_date{$branch} and $branch_date{$branch} >= $date) {
604                         # skip
605                         print "skip patchset $patchset: $date before $branch_date{$branch}\n";
606                         $state = 11;
607                         next;
608                 }
609                 if($ancestor) {
610                         if(-f "$git_dir/refs/heads/$branch") {
611                                 print STDERR "Branch $branch already exists!\n";
612                                 $state=11;
613                                 next;
614                         }
615                         unless(open(H,"$git_dir/refs/heads/$ancestor")) {
616                                 print STDERR "Branch $ancestor does not exist!\n";
617                                 $state=11;
618                                 next;
619                         }
620                         chomp(my $id = <H>);
621                         close(H);
622                         unless(open(H,"> $git_dir/refs/heads/$branch")) {
623                                 print STDERR "Could not create branch $branch: $!\n";
624                                 $state=11;
625                                 next;
626                         }
627                         print H "$id\n"
628                                 or die "Could not write branch $branch: $!";
629                         close(H)
630                                 or die "Could not write branch $branch: $!";
631                 }
632                 if(($ancestor || $branch) ne $last_branch) {
633                         print "Switching from $last_branch to $branch\n" if $opt_v;
634                         system("git-read-tree","-m","-u","$last_branch","$branch");
635                         die "read-tree failed: $?\n" if $?;
636                 }
637                 if($branch ne $last_branch) {
638                         unlink("$git_dir/HEAD");
639                         symlink("refs/heads/$branch","$git_dir/HEAD");
640                         $last_branch = $branch;
641                 }
642                 $state = 9;
643         } elsif($state == 8) {
644                 $logmsg .= "$_\n";
645         } elsif($state == 9 and /^\s+(\S+):(INITIAL|\d(?:\.\d+)+)->(\d(?:\.\d+)+)\s*$/) {
646 #       VERSION:1.96->1.96.2.1
647                 my $init = ($2 eq "INITIAL");
648                 my $fn = $1;
649                 my $rev = $3;
650                 $fn =~ s#^/+##;
651                 my $data = $cvs->file($fn,$rev);
652                 print "".($init ? "New" : "Update")." $fn: ".length($data)." bytes.\n";
653                 mkpath(dirname($fn),$opt_v);
654                 open(F,"> ./$fn")
655                         or die "Cannot create '$fn': $!\n";
656                 print F $data
657                         or die "Cannot write to '$fn': $!\n";
658                 close(F)
659                         or die "Cannot write to '$fn': $!\n";
660                 chmod(pmode($cvs->{'mode'}), $fn);
661                 push(@new,$fn); # may be resurrected!
662         } elsif($state == 9 and /^\s+(\S+):\d(?:\.\d+)+->(\d(?:\.\d+)+)\(DEAD\)\s*$/) {
663                 my $fn = $1;
664                 $fn =~ s#^/+##;
665                 push(@old,$fn);
666         } elsif($state == 9 and /^\s*$/) {
667                 $state = 10;
668         } elsif(($state == 9 or $state == 10) and /^-+$/) {
669                 &$commit();
670                 $state = 1;
671         } elsif($state == 11 and /^-+$/) {
672                 $state = 1;
673         } elsif(/^-+$/) { # end of unknown-line processing
674                 $state = 1;
675         } elsif($state != 11) { # ignore stuff when skipping
676                 print "* UNKNOWN LINE * $_\n";
677         }
678 }
679 &$commit() if $branch and $state != 11;
680
681 # Now switch back to the branch we were in before all of this happened
682 if($orig_branch) {
683         print "DONE; switching back to $orig_branch\n" if $opt_v;
684 } else {
685         $orig_branch = "master";
686         print "DONE; creating $orig_branch branch\n" if $opt_v;
687         system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
688                 unless -f "$git_dir/refs/heads/master";
689 }
690
691 system("git-read-tree","-m","-u","$last_branch","$orig_branch");
692 die "read-tree failed: $?\n" if $?;
693
694 unlink("$git_dir/HEAD");
695 symlink("refs/heads/$orig_branch","$git_dir/HEAD");
696