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