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