9773891b5ddbafbbaea4425f594b131679dfa5b7
1 #!/usr/bin/perl -w
3 # This tool is copyright (c) 2005, Matthias Urlichs.
4 # It is released under the Gnu Public License, version 2.
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.
16 use strict;
17 use warnings;
18 use Getopt::Std;
19 use File::Spec;
20 use File::Temp qw(tempfile);
21 use File::Path qw(mkpath);
22 use File::Basename qw(basename dirname);
23 use Time::Local;
24 use IO::Socket;
25 use IO::Pipe;
26 use POSIX qw(strftime dup2);
28 $SIG{'PIPE'}="IGNORE";
29 $ENV{'TZ'}="UTC";
31 our($opt_h,$opt_o,$opt_v,$opt_d,$opt_p,$opt_C,$opt_z);
33 sub usage() {
34 print STDERR <<END;
35 Usage: ${\basename $0} # fetch/update GIT from CVS
36 [ -o branch-for-HEAD ] [ -h ] [ -v ] [ -d CVSROOT ]
37 [ -p opts-for-cvsps ] [ -C GIT_repository ] [ -z fuzz ]
38 [ CVS_module ]
39 END
40 exit(1);
41 }
43 getopts("hqvo:d:p:C:z:") or usage();
44 usage if $opt_h;
46 @ARGV <= 1 or usage();
48 if($opt_d) {
49 $ENV{"CVSROOT"} = $opt_d;
50 } elsif(-f 'CVS/Root') {
51 open my $f, '<', 'CVS/Root' or die 'Failed to open CVS/Root';
52 $opt_d = <$f>;
53 chomp $opt_d;
54 close $f;
55 $ENV{"CVSROOT"} = $opt_d;
56 } elsif($ENV{"CVSROOT"}) {
57 $opt_d = $ENV{"CVSROOT"};
58 } else {
59 die "CVSROOT needs to be set";
60 }
61 $opt_o ||= "origin";
62 my $git_tree = $opt_C;
63 $git_tree ||= ".";
65 my $cvs_tree;
66 if ($#ARGV == 0) {
67 $cvs_tree = $ARGV[0];
68 } elsif (-f 'CVS/Repository') {
69 open my $f, '<', 'CVS/Repository' or
70 die 'Failed to open CVS/Repository';
71 $cvs_tree = <$f>;
72 chomp $cvs_tree;
73 close $f
74 } else {
75 usage();
76 }
78 select(STDERR); $|=1; select(STDOUT);
81 package CVSconn;
82 # Basic CVS dialog.
83 # We're only interested in connecting and downloading, so ...
85 use File::Spec;
86 use File::Temp qw(tempfile);
87 use POSIX qw(strftime dup2);
89 sub new {
90 my($what,$repo,$subdir) = @_;
91 $what=ref($what) if ref($what);
93 my $self = {};
94 $self->{'buffer'} = "";
95 bless($self,$what);
97 $repo =~ s#/+$##;
98 $self->{'fullrep'} = $repo;
99 $self->conn();
101 $self->{'subdir'} = $subdir;
102 $self->{'lines'} = undef;
104 return $self;
105 }
107 sub conn {
108 my $self = shift;
109 my $repo = $self->{'fullrep'};
110 if($repo =~ s/^:pserver:(?:(.*?)(?::(.*?))?@)?([^:\/]*)(?::(\d*))?//) {
111 my($user,$pass,$serv,$port) = ($1,$2,$3,$4);
112 $user="anonymous" unless defined $user;
113 my $rr2 = "-";
114 unless($port) {
115 $rr2 = ":pserver:$user\@$serv:$repo";
116 $port=2401;
117 }
118 my $rr = ":pserver:$user\@$serv:$port$repo";
120 unless($pass) {
121 open(H,$ENV{'HOME'}."/.cvspass") and do {
122 # :pserver:cvs@mea.tmt.tele.fi:/cvsroot/zmailer Ah<Z
123 while(<H>) {
124 chomp;
125 s/^\/\d+\s+//;
126 my ($w,$p) = split(/\s/,$_,2);
127 if($w eq $rr or $w eq $rr2) {
128 $pass = $p;
129 last;
130 }
131 }
132 };
133 }
134 $pass="A" unless $pass;
136 my $s = IO::Socket::INET->new(PeerHost => $serv, PeerPort => $port);
137 die "Socket to $serv: $!\n" unless defined $s;
138 $s->write("BEGIN AUTH REQUEST\n$repo\n$user\n$pass\nEND AUTH REQUEST\n")
139 or die "Write to $serv: $!\n";
140 $s->flush();
142 my $rep = <$s>;
144 if($rep ne "I LOVE YOU\n") {
145 $rep="<unknown>" unless $rep;
146 die "AuthReply: $rep\n";
147 }
148 $self->{'socketo'} = $s;
149 $self->{'socketi'} = $s;
150 } else { # local or ext: Fork off our own cvs server.
151 my $pr = IO::Pipe->new();
152 my $pw = IO::Pipe->new();
153 my $pid = fork();
154 die "Fork: $!\n" unless defined $pid;
155 my $cvs = 'cvs';
156 $cvs = $ENV{CVS_SERVER} if exists $ENV{CVS_SERVER};
157 my $rsh = 'rsh';
158 $rsh = $ENV{CVS_RSH} if exists $ENV{CVS_RSH};
160 my @cvs = ($cvs, 'server');
161 my ($local, $user, $host);
162 $local = $repo =~ s/:local://;
163 if (!$local) {
164 $repo =~ s/:ext://;
165 $local = !($repo =~ s/^(?:([^\@:]+)\@)?([^:]+)://);
166 ($user, $host) = ($1, $2);
167 }
168 if (!$local) {
169 if ($user) {
170 unshift @cvs, $rsh, '-l', $user, $host;
171 } else {
172 unshift @cvs, $rsh, $host;
173 }
174 }
176 unless($pid) {
177 $pr->writer();
178 $pw->reader();
179 dup2($pw->fileno(),0);
180 dup2($pr->fileno(),1);
181 $pr->close();
182 $pw->close();
183 exec(@cvs);
184 }
185 $pw->writer();
186 $pr->reader();
187 $self->{'socketo'} = $pw;
188 $self->{'socketi'} = $pr;
189 }
190 $self->{'socketo'}->write("Root $repo\n");
192 # Trial and error says that this probably is the minimum set
193 $self->{'socketo'}->write("Valid-responses ok error Valid-requests Mode M Mbinary E F Checked-in Created Updated Merged Removed\n");
195 $self->{'socketo'}->write("valid-requests\n");
196 $self->{'socketo'}->flush();
198 chomp(my $rep=$self->readline());
199 if($rep !~ s/^Valid-requests\s*//) {
200 $rep="<unknown>" unless $rep;
201 die "Expected Valid-requests from server, but got: $rep\n";
202 }
203 chomp(my $res=$self->readline());
204 die "validReply: $res\n" if $res ne "ok";
206 $self->{'socketo'}->write("UseUnchanged\n") if $rep =~ /\bUseUnchanged\b/;
207 $self->{'repo'} = $repo;
208 }
210 sub readline {
211 my($self) = @_;
212 return $self->{'socketi'}->getline();
213 }
215 sub _file {
216 # Request a file with a given revision.
217 # Trial and error says this is a good way to do it. :-/
218 my($self,$fn,$rev) = @_;
219 $self->{'socketo'}->write("Argument -N\n") or return undef;
220 $self->{'socketo'}->write("Argument -P\n") or return undef;
221 # $self->{'socketo'}->write("Argument -ko\n") or return undef;
222 # -ko: Linus' version doesn't use it
223 $self->{'socketo'}->write("Argument -r\n") or return undef;
224 $self->{'socketo'}->write("Argument $rev\n") or return undef;
225 $self->{'socketo'}->write("Argument --\n") or return undef;
226 $self->{'socketo'}->write("Argument $self->{'subdir'}/$fn\n") or return undef;
227 $self->{'socketo'}->write("Directory .\n") or return undef;
228 $self->{'socketo'}->write("$self->{'repo'}\n") or return undef;
229 # $self->{'socketo'}->write("Sticky T1.0\n") or return undef;
230 $self->{'socketo'}->write("co\n") or return undef;
231 $self->{'socketo'}->flush() or return undef;
232 $self->{'lines'} = 0;
233 return 1;
234 }
235 sub _line {
236 # Read a line from the server.
237 # ... except that 'line' may be an entire file. ;-)
238 my($self, $fh) = @_;
239 die "Not in lines" unless defined $self->{'lines'};
241 my $line;
242 my $res=0;
243 while(defined($line = $self->readline())) {
244 # M U gnupg-cvs-rep/AUTHORS
245 # Updated gnupg-cvs-rep/
246 # /daten/src/rsync/gnupg-cvs-rep/AUTHORS
247 # /AUTHORS/1.1///T1.1
248 # u=rw,g=rw,o=rw
249 # 0
250 # ok
252 if($line =~ s/^(?:Created|Updated) //) {
253 $line = $self->readline(); # path
254 $line = $self->readline(); # Entries line
255 my $mode = $self->readline(); chomp $mode;
256 $self->{'mode'} = $mode;
257 defined (my $cnt = $self->readline())
258 or die "EOF from server after 'Changed'\n";
259 chomp $cnt;
260 die "Duh: Filesize $cnt" if $cnt !~ /^\d+$/;
261 $line="";
262 $res=0;
263 while($cnt) {
264 my $buf;
265 my $num = $self->{'socketi'}->read($buf,$cnt);
266 die "Server: Filesize $cnt: $num: $!\n" if not defined $num or $num<=0;
267 print $fh $buf;
268 $res += $num;
269 $cnt -= $num;
270 }
271 } elsif($line =~ s/^ //) {
272 print $fh $line;
273 $res += length($line);
274 } elsif($line =~ /^M\b/) {
275 # output, do nothing
276 } elsif($line =~ /^Mbinary\b/) {
277 my $cnt;
278 die "EOF from server after 'Mbinary'" unless defined ($cnt = $self->readline());
279 chomp $cnt;
280 die "Duh: Mbinary $cnt" if $cnt !~ /^\d+$/ or $cnt<1;
281 $line="";
282 while($cnt) {
283 my $buf;
284 my $num = $self->{'socketi'}->read($buf,$cnt);
285 die "S: Mbinary $cnt: $num: $!\n" if not defined $num or $num<=0;
286 print $fh $buf;
287 $res += $num;
288 $cnt -= $num;
289 }
290 } else {
291 chomp $line;
292 if($line eq "ok") {
293 # print STDERR "S: ok (".length($res).")\n";
294 return $res;
295 } elsif($line =~ s/^E //) {
296 # print STDERR "S: $line\n";
297 } else {
298 die "Unknown: $line\n";
299 }
300 }
301 }
302 }
303 sub file {
304 my($self,$fn,$rev) = @_;
305 my $res;
307 my ($fh, $name) = tempfile('gitcvs.XXXXXX',
308 DIR => File::Spec->tmpdir(), UNLINK => 1);
310 $self->_file($fn,$rev) and $res = $self->_line($fh);
312 if (!defined $res) {
313 # retry
314 $self->conn();
315 $self->_file($fn,$rev)
316 or die "No file command send\n";
317 $res = $self->_line($fh);
318 die "No input: $fn $rev\n" unless defined $res;
319 }
321 return ($name, $res);
322 }
325 package main;
327 my $cvs = CVSconn->new($opt_d, $cvs_tree);
330 sub pdate($) {
331 my($d) = @_;
332 m#(\d{2,4})/(\d\d)/(\d\d)\s(\d\d):(\d\d)(?::(\d\d))?#
333 or die "Unparseable date: $d\n";
334 my $y=$1; $y-=1900 if $y>1900;
335 return timegm($6||0,$5,$4,$3,$2-1,$y);
336 }
338 sub pmode($) {
339 my($mode) = @_;
340 my $m = 0;
341 my $mm = 0;
342 my $um = 0;
343 for my $x(split(//,$mode)) {
344 if($x eq ",") {
345 $m |= $mm&$um;
346 $mm = 0;
347 $um = 0;
348 } elsif($x eq "u") { $um |= 0700;
349 } elsif($x eq "g") { $um |= 0070;
350 } elsif($x eq "o") { $um |= 0007;
351 } elsif($x eq "r") { $mm |= 0444;
352 } elsif($x eq "w") { $mm |= 0222;
353 } elsif($x eq "x") { $mm |= 0111;
354 } elsif($x eq "=") { # do nothing
355 } else { die "Unknown mode: $mode\n";
356 }
357 }
358 $m |= $mm&$um;
359 return $m;
360 }
362 my $tmpcv = "/var/cache/cvs";
364 sub getwd() {
365 my $pwd = `pwd`;
366 chomp $pwd;
367 return $pwd;
368 }
370 -d $git_tree
371 or mkdir($git_tree,0777)
372 or die "Could not create $git_tree: $!";
373 chdir($git_tree);
375 my $last_branch = "";
376 my $orig_branch = "";
377 my %branch_date;
379 my $git_dir = $ENV{"GIT_DIR"} || ".git";
380 $git_dir = getwd()."/".$git_dir unless $git_dir =~ m#^/#;
381 $ENV{"GIT_DIR"} = $git_dir;
382 my $orig_git_index;
383 $orig_git_index = $ENV{GIT_INDEX_FILE} if exists $ENV{GIT_INDEX_FILE};
384 my ($git_ih, $git_index) = tempfile('gitXXXXXX', SUFFIX => '.idx',
385 DIR => File::Spec->tmpdir());
386 close ($git_ih);
387 $ENV{GIT_INDEX_FILE} = $git_index;
388 unless(-d $git_dir) {
389 system("git-init-db");
390 die "Cannot init the GIT db at $git_tree: $?\n" if $?;
391 system("git-read-tree");
392 die "Cannot init an empty tree: $?\n" if $?;
394 $last_branch = $opt_o;
395 $orig_branch = "";
396 } else {
397 -f "$git_dir/refs/heads/$opt_o"
398 or die "Branch '$opt_o' does not exist.\n".
399 "Either use the correct '-o branch' option,\n".
400 "or import to a new repository.\n";
402 $last_branch = basename(readlink("$git_dir/HEAD"));
403 unless($last_branch) {
404 warn "Cannot read the last branch name: $! -- assuming 'master'\n";
405 $last_branch = "master";
406 }
407 $orig_branch = $last_branch;
409 # populate index
410 system('git-read-tree', $last_branch);
412 # Get the last import timestamps
413 opendir(D,"$git_dir/refs/heads");
414 while(defined(my $head = readdir(D))) {
415 next if $head =~ /^\./;
416 open(F,"$git_dir/refs/heads/$head")
417 or die "Bad head branch: $head: $!\n";
418 chomp(my $ftag = <F>);
419 close(F);
420 open(F,"git-cat-file commit $ftag |");
421 while(<F>) {
422 next unless /^author\s.*\s(\d+)\s[-+]\d{4}$/;
423 $branch_date{$head} = $1;
424 last;
425 }
426 close(F);
427 }
428 closedir(D);
429 }
431 -d $git_dir
432 or die "Could not create git subdir ($git_dir).\n";
434 my $pid = open(CVS,"-|");
435 die "Cannot fork: $!\n" unless defined $pid;
436 unless($pid) {
437 my @opt;
438 @opt = split(/,/,$opt_p) if defined $opt_p;
439 unshift @opt, '-z', $opt_z if defined $opt_z;
440 exec("cvsps",@opt,"-u","-A","--cvs-direct",'--root',$opt_d,$cvs_tree);
441 die "Could not start cvsps: $!\n";
442 }
445 ## cvsps output:
446 #---------------------
447 #PatchSet 314
448 #Date: 1999/09/18 13:03:59
449 #Author: wkoch
450 #Branch: STABLE-BRANCH-1-0
451 #Ancestor branch: HEAD
452 #Tag: (none)
453 #Log:
454 # See ChangeLog: Sat Sep 18 13:03:28 CEST 1999 Werner Koch
455 #Members:
456 # README:1.57->1.57.2.1
457 # VERSION:1.96->1.96.2.1
458 #
459 #---------------------
461 my $state = 0;
463 my($patchset,$date,$author,$branch,$ancestor,$tag,$logmsg);
464 my(@old,@new);
465 my $commit = sub {
466 my $pid;
467 while(@old) {
468 my @o2;
469 if(@old > 55) {
470 @o2 = splice(@old,0,50);
471 } else {
472 @o2 = @old;
473 @old = ();
474 }
475 system("git-update-cache","--force-remove","--",@o2);
476 die "Cannot remove files: $?\n" if $?;
477 }
478 while(@new) {
479 my @n2;
480 if(@new > 12) {
481 @n2 = splice(@new,0,10);
482 } else {
483 @n2 = @new;
484 @new = ();
485 }
486 system("git-update-cache","--add",
487 (map { ('--cacheinfo', @$_) } @n2));
488 die "Cannot add files: $?\n" if $?;
489 }
491 $pid = open(C,"-|");
492 die "Cannot fork: $!" unless defined $pid;
493 unless($pid) {
494 exec("git-write-tree");
495 die "Cannot exec git-write-tree: $!\n";
496 }
497 chomp(my $tree = <C>);
498 length($tree) == 40
499 or die "Cannot get tree id ($tree): $!\n";
500 close(C)
501 or die "Error running git-write-tree: $?\n";
502 print "Tree ID $tree\n" if $opt_v;
504 my $parent = "";
505 if(open(C,"$git_dir/refs/heads/$last_branch")) {
506 chomp($parent = <C>);
507 close(C);
508 length($parent) == 40
509 or die "Cannot get parent id ($parent): $!\n";
510 print "Parent ID $parent\n" if $opt_v;
511 }
513 my $pr = IO::Pipe->new();
514 my $pw = IO::Pipe->new();
515 $pid = fork();
516 die "Fork: $!\n" unless defined $pid;
517 unless($pid) {
518 $pr->writer();
519 $pw->reader();
520 dup2($pw->fileno(),0);
521 dup2($pr->fileno(),1);
522 $pr->close();
523 $pw->close();
525 my @par = ();
526 @par = ("-p",$parent) if $parent;
527 exec("env",
528 "GIT_AUTHOR_NAME=$author",
529 "GIT_AUTHOR_EMAIL=$author",
530 "GIT_AUTHOR_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
531 "GIT_COMMITTER_NAME=$author",
532 "GIT_COMMITTER_EMAIL=$author",
533 "GIT_COMMITTER_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
534 "git-commit-tree", $tree,@par);
535 die "Cannot exec git-commit-tree: $!\n";
536 }
537 $pw->writer();
538 $pr->reader();
540 # compatibility with git2cvs
541 substr($logmsg,32767) = "" if length($logmsg) > 32767;
542 $logmsg =~ s/[\s\n]+\z//;
544 print $pw "$logmsg\n"
545 or die "Error writing to git-commit-tree: $!\n";
546 $pw->close();
548 print "Committed patch $patchset ($branch)\n" if $opt_v;
549 chomp(my $cid = <$pr>);
550 length($cid) == 40
551 or die "Cannot get commit id ($cid): $!\n";
552 print "Commit ID $cid\n" if $opt_v;
553 $pr->close();
555 waitpid($pid,0);
556 die "Error running git-commit-tree: $?\n" if $?;
558 open(C,">$git_dir/refs/heads/$branch")
559 or die "Cannot open branch $branch for update: $!\n";
560 print C "$cid\n"
561 or die "Cannot write branch $branch for update: $!\n";
562 close(C)
563 or die "Cannot write branch $branch for update: $!\n";
565 if($tag) {
566 open(C,">$git_dir/refs/tags/$tag")
567 or die "Cannot create tag $tag: $!\n";
568 print C "$cid\n"
569 or die "Cannot write tag $branch: $!\n";
570 close(C)
571 or die "Cannot write tag $branch: $!\n";
572 print "Created tag '$tag' on '$branch'\n" if $opt_v;
573 }
574 };
576 while(<CVS>) {
577 chomp;
578 if($state == 0 and /^-+$/) {
579 $state = 1;
580 } elsif($state == 0) {
581 $state = 1;
582 redo;
583 } elsif(($state==0 or $state==1) and s/^PatchSet\s+//) {
584 $patchset = 0+$_;
585 $state=2;
586 } elsif($state == 2 and s/^Date:\s+//) {
587 $date = pdate($_);
588 unless($date) {
589 print STDERR "Could not parse date: $_\n";
590 $state=0;
591 next;
592 }
593 $state=3;
594 } elsif($state == 3 and s/^Author:\s+//) {
595 s/\s+$//;
596 $author = $_;
597 $state = 4;
598 } elsif($state == 4 and s/^Branch:\s+//) {
599 s/\s+$//;
600 $branch = $_;
601 $state = 5;
602 } elsif($state == 5 and s/^Ancestor branch:\s+//) {
603 s/\s+$//;
604 $ancestor = $_;
605 $ancestor = $opt_o if $ancestor eq "HEAD";
606 $state = 6;
607 } elsif($state == 5) {
608 $ancestor = undef;
609 $state = 6;
610 redo;
611 } elsif($state == 6 and s/^Tag:\s+//) {
612 s/\s+$//;
613 if($_ eq "(none)") {
614 $tag = undef;
615 } else {
616 $tag = $_;
617 }
618 $state = 7;
619 } elsif($state == 7 and /^Log:/) {
620 $logmsg = "";
621 $state = 8;
622 } elsif($state == 8 and /^Members:/) {
623 $branch = $opt_o if $branch eq "HEAD";
624 if(defined $branch_date{$branch} and $branch_date{$branch} >= $date) {
625 # skip
626 print "skip patchset $patchset: $date before $branch_date{$branch}\n" if $opt_v;
627 $state = 11;
628 next;
629 }
630 if($ancestor) {
631 if(-f "$git_dir/refs/heads/$branch") {
632 print STDERR "Branch $branch already exists!\n";
633 $state=11;
634 next;
635 }
636 unless(open(H,"$git_dir/refs/heads/$ancestor")) {
637 print STDERR "Branch $ancestor does not exist!\n";
638 $state=11;
639 next;
640 }
641 chomp(my $id = <H>);
642 close(H);
643 unless(open(H,"> $git_dir/refs/heads/$branch")) {
644 print STDERR "Could not create branch $branch: $!\n";
645 $state=11;
646 next;
647 }
648 print H "$id\n"
649 or die "Could not write branch $branch: $!";
650 close(H)
651 or die "Could not write branch $branch: $!";
652 }
653 if(($ancestor || $branch) ne $last_branch) {
654 print "Switching from $last_branch to $branch\n" if $opt_v;
655 system("git-read-tree", $branch);
656 die "read-tree failed: $?\n" if $?;
657 }
658 $last_branch = $branch if $branch ne $last_branch;
659 $state = 9;
660 } elsif($state == 8) {
661 $logmsg .= "$_\n";
662 } elsif($state == 9 and /^\s+(\S+):(INITIAL|\d(?:\.\d+)+)->(\d(?:\.\d+)+)\s*$/) {
663 # VERSION:1.96->1.96.2.1
664 my $init = ($2 eq "INITIAL");
665 my $fn = $1;
666 my $rev = $3;
667 $fn =~ s#^/+##;
668 my ($tmpname, $size) = $cvs->file($fn,$rev);
669 print "".($init ? "New" : "Update")." $fn: $size bytes.\n" if $opt_v;
670 open my $F, '-|', "git-write-blob $tmpname"
671 or die "Cannot create object: $!\n";
672 my $sha = <$F>;
673 chomp $sha;
674 close $F;
675 unlink($tmpname);
676 my $mode = pmode($cvs->{'mode'});
677 push(@new,[$mode, $sha, $fn]); # may be resurrected!
678 } elsif($state == 9 and /^\s+(\S+):\d(?:\.\d+)+->(\d(?:\.\d+)+)\(DEAD\)\s*$/) {
679 my $fn = $1;
680 $fn =~ s#^/+##;
681 push(@old,$fn);
682 } elsif($state == 9 and /^\s*$/) {
683 $state = 10;
684 } elsif(($state == 9 or $state == 10) and /^-+$/) {
685 &$commit();
686 $state = 1;
687 } elsif($state == 11 and /^-+$/) {
688 $state = 1;
689 } elsif(/^-+$/) { # end of unknown-line processing
690 $state = 1;
691 } elsif($state != 11) { # ignore stuff when skipping
692 print "* UNKNOWN LINE * $_\n";
693 }
694 }
695 &$commit() if $branch and $state != 11;
697 unlink($git_index);
699 # Now switch back to the branch we were in before all of this happened
700 if($orig_branch) {
701 print "DONE\n" if $opt_v;
702 } else {
703 $orig_branch = "master";
704 print "DONE; creating $orig_branch branch\n" if $opt_v;
705 system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
706 unless -f "$git_dir/refs/heads/master";
707 unlink("$git_dir/HEAD");
708 symlink("refs/heads/$orig_branch","$git_dir/HEAD");
709 if (defined $orig_git_index) {
710 $ENV{GIT_INDEX_FILE} = $orig_git_index;
711 } else {
712 delete $ENV{GIT_INDEX_FILE};
713 }
714 system('git checkout');
715 die "checkout failed: $?\n" if $?;
716 }