]> git.ipfire.org Git - thirdparty/git.git/blame - git-cvsimport.perl
cvsimport: catch error condition where cvs host disappears
[thirdparty/git.git] / git-cvsimport.perl
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;
79ee456c
SV
19use File::Spec;
20use File::Temp qw(tempfile);
a57a9493
MU
21use File::Path qw(mkpath);
22use File::Basename qw(basename dirname);
23use Time::Local;
2a3e1a85
MU
24use IO::Socket;
25use IO::Pipe;
26use POSIX qw(strftime dup2);
0d821d4d 27use IPC::Open2;
a57a9493
MU
28
29$SIG{'PIPE'}="IGNORE";
30$ENV{'TZ'}="UTC";
31
0d821d4d 32our($opt_h,$opt_o,$opt_v,$opt_k,$opt_u,$opt_d,$opt_p,$opt_C,$opt_z,$opt_i,$opt_s,$opt_m,$opt_M);
a57a9493
MU
33
34sub usage() {
35 print STDERR <<END;
2a3e1a85 36Usage: ${\basename $0} # fetch/update GIT from CVS
0d821d4d
PA
37 [-o branch-for-HEAD] [-h] [-v] [-d CVSROOT]
38 [-p opts-for-cvsps] [-C GIT_repository] [-z fuzz]
39 [-i] [-k] [-u] [-s subst] [-m] [-M regex] [CVS_module]
a57a9493
MU
40END
41 exit(1);
42}
43
0d821d4d 44getopts("hivmkuo:d:p:C:z:s:M:") or usage();
a57a9493
MU
45usage if $opt_h;
46
f9714a4a 47@ARGV <= 1 or usage();
a57a9493 48
2a3e1a85
MU
49if($opt_d) {
50 $ENV{"CVSROOT"} = $opt_d;
f9714a4a
SV
51} elsif(-f 'CVS/Root') {
52 open my $f, '<', 'CVS/Root' or die 'Failed to open CVS/Root';
53 $opt_d = <$f>;
54 chomp $opt_d;
55 close $f;
56 $ENV{"CVSROOT"} = $opt_d;
2a3e1a85
MU
57} elsif($ENV{"CVSROOT"}) {
58 $opt_d = $ENV{"CVSROOT"};
59} else {
60 die "CVSROOT needs to be set";
61}
62$opt_o ||= "origin";
fbfd60d6 63$opt_s ||= "-";
f9714a4a 64my $git_tree = $opt_C;
2a3e1a85
MU
65$git_tree ||= ".";
66
f9714a4a
SV
67my $cvs_tree;
68if ($#ARGV == 0) {
69 $cvs_tree = $ARGV[0];
70} elsif (-f 'CVS/Repository') {
71 open my $f, '<', 'CVS/Repository' or
72 die 'Failed to open CVS/Repository';
73 $cvs_tree = <$f>;
74 chomp $cvs_tree;
db4b6582 75 close $f;
f9714a4a
SV
76} else {
77 usage();
78}
79
db4b6582
ML
80our @mergerx = ();
81if ($opt_m) {
82 @mergerx = ( qr/\W(?:from|of|merge|merging|merged) (\w+)/i );
83}
84if ($opt_M) {
85 push (@mergerx, qr/$opt_M/);
86}
87
a57a9493
MU
88select(STDERR); $|=1; select(STDOUT);
89
90
91package CVSconn;
92# Basic CVS dialog.
2a3e1a85 93# We're only interested in connecting and downloading, so ...
a57a9493 94
2eb6d82e
SV
95use File::Spec;
96use File::Temp qw(tempfile);
f65ae603
MU
97use POSIX qw(strftime dup2);
98
a57a9493
MU
99sub new {
100 my($what,$repo,$subdir) = @_;
101 $what=ref($what) if ref($what);
102
103 my $self = {};
104 $self->{'buffer'} = "";
105 bless($self,$what);
106
107 $repo =~ s#/+$##;
108 $self->{'fullrep'} = $repo;
109 $self->conn();
110
111 $self->{'subdir'} = $subdir;
112 $self->{'lines'} = undef;
113
114 return $self;
115}
116
117sub conn {
118 my $self = shift;
119 my $repo = $self->{'fullrep'};
120 if($repo =~ s/^:pserver:(?:(.*?)(?::(.*?))?@)?([^:\/]*)(?::(\d*))?//) {
121 my($user,$pass,$serv,$port) = ($1,$2,$3,$4);
122 $user="anonymous" unless defined $user;
2a3e1a85 123 my $rr2 = "-";
a57a9493
MU
124 unless($port) {
125 $rr2 = ":pserver:$user\@$serv:$repo";
126 $port=2401;
127 }
128 my $rr = ":pserver:$user\@$serv:$port$repo";
129
130 unless($pass) {
131 open(H,$ENV{'HOME'}."/.cvspass") and do {
132 # :pserver:cvs@mea.tmt.tele.fi:/cvsroot/zmailer Ah<Z
133 while(<H>) {
134 chomp;
135 s/^\/\d+\s+//;
136 my ($w,$p) = split(/\s/,$_,2);
137 if($w eq $rr or $w eq $rr2) {
138 $pass = $p;
139 last;
140 }
141 }
142 };
143 }
144 $pass="A" unless $pass;
145
a57a9493
MU
146 my $s = IO::Socket::INET->new(PeerHost => $serv, PeerPort => $port);
147 die "Socket to $serv: $!\n" unless defined $s;
148 $s->write("BEGIN AUTH REQUEST\n$repo\n$user\n$pass\nEND AUTH REQUEST\n")
149 or die "Write to $serv: $!\n";
150 $s->flush();
151
152 my $rep = <$s>;
153
154 if($rep ne "I LOVE YOU\n") {
155 $rep="<unknown>" unless $rep;
156 die "AuthReply: $rep\n";
157 }
158 $self->{'socketo'} = $s;
159 $self->{'socketi'} = $s;
34155390 160 } else { # local or ext: Fork off our own cvs server.
a57a9493
MU
161 my $pr = IO::Pipe->new();
162 my $pw = IO::Pipe->new();
163 my $pid = fork();
164 die "Fork: $!\n" unless defined $pid;
8d0ea311
SV
165 my $cvs = 'cvs';
166 $cvs = $ENV{CVS_SERVER} if exists $ENV{CVS_SERVER};
34155390
SV
167 my $rsh = 'rsh';
168 $rsh = $ENV{CVS_RSH} if exists $ENV{CVS_RSH};
169
170 my @cvs = ($cvs, 'server');
171 my ($local, $user, $host);
172 $local = $repo =~ s/:local://;
173 if (!$local) {
174 $repo =~ s/:ext://;
175 $local = !($repo =~ s/^(?:([^\@:]+)\@)?([^:]+)://);
176 ($user, $host) = ($1, $2);
177 }
178 if (!$local) {
179 if ($user) {
180 unshift @cvs, $rsh, '-l', $user, $host;
181 } else {
182 unshift @cvs, $rsh, $host;
183 }
184 }
185
a57a9493
MU
186 unless($pid) {
187 $pr->writer();
188 $pw->reader();
a57a9493
MU
189 dup2($pw->fileno(),0);
190 dup2($pr->fileno(),1);
191 $pr->close();
192 $pw->close();
34155390 193 exec(@cvs);
a57a9493
MU
194 }
195 $pw->writer();
196 $pr->reader();
197 $self->{'socketo'} = $pw;
198 $self->{'socketi'} = $pr;
199 }
200 $self->{'socketo'}->write("Root $repo\n");
201
202 # Trial and error says that this probably is the minimum set
b0921331 203 $self->{'socketo'}->write("Valid-responses ok error Valid-requests Mode M Mbinary E Checked-in Created Updated Merged Removed\n");
a57a9493
MU
204
205 $self->{'socketo'}->write("valid-requests\n");
206 $self->{'socketo'}->flush();
207
208 chomp(my $rep=$self->readline());
209 if($rep !~ s/^Valid-requests\s*//) {
210 $rep="<unknown>" unless $rep;
211 die "Expected Valid-requests from server, but got: $rep\n";
212 }
213 chomp(my $res=$self->readline());
214 die "validReply: $res\n" if $res ne "ok";
215
216 $self->{'socketo'}->write("UseUnchanged\n") if $rep =~ /\bUseUnchanged\b/;
217 $self->{'repo'} = $repo;
218}
219
220sub readline {
221 my($self) = @_;
222 return $self->{'socketi'}->getline();
223}
224
225sub _file {
226 # Request a file with a given revision.
227 # Trial and error says this is a good way to do it. :-/
228 my($self,$fn,$rev) = @_;
229 $self->{'socketo'}->write("Argument -N\n") or return undef;
230 $self->{'socketo'}->write("Argument -P\n") or return undef;
abe05822
ML
231 # -kk: Linus' version doesn't use it - defaults to off
232 if ($opt_k) {
233 $self->{'socketo'}->write("Argument -kk\n") or return undef;
234 }
a57a9493
MU
235 $self->{'socketo'}->write("Argument -r\n") or return undef;
236 $self->{'socketo'}->write("Argument $rev\n") or return undef;
237 $self->{'socketo'}->write("Argument --\n") or return undef;
238 $self->{'socketo'}->write("Argument $self->{'subdir'}/$fn\n") or return undef;
239 $self->{'socketo'}->write("Directory .\n") or return undef;
240 $self->{'socketo'}->write("$self->{'repo'}\n") or return undef;
4f7c0caa 241 # $self->{'socketo'}->write("Sticky T1.0\n") or return undef;
a57a9493
MU
242 $self->{'socketo'}->write("co\n") or return undef;
243 $self->{'socketo'}->flush() or return undef;
244 $self->{'lines'} = 0;
245 return 1;
246}
247sub _line {
248 # Read a line from the server.
249 # ... except that 'line' may be an entire file. ;-)
2eb6d82e 250 my($self, $fh) = @_;
a57a9493
MU
251 die "Not in lines" unless defined $self->{'lines'};
252
253 my $line;
2eb6d82e 254 my $res=0;
a57a9493
MU
255 while(defined($line = $self->readline())) {
256 # M U gnupg-cvs-rep/AUTHORS
257 # Updated gnupg-cvs-rep/
258 # /daten/src/rsync/gnupg-cvs-rep/AUTHORS
259 # /AUTHORS/1.1///T1.1
260 # u=rw,g=rw,o=rw
261 # 0
262 # ok
263
264 if($line =~ s/^(?:Created|Updated) //) {
265 $line = $self->readline(); # path
266 $line = $self->readline(); # Entries line
267 my $mode = $self->readline(); chomp $mode;
268 $self->{'mode'} = $mode;
269 defined (my $cnt = $self->readline())
270 or die "EOF from server after 'Changed'\n";
271 chomp $cnt;
272 die "Duh: Filesize $cnt" if $cnt !~ /^\d+$/;
273 $line="";
2eb6d82e 274 $res=0;
a57a9493
MU
275 while($cnt) {
276 my $buf;
277 my $num = $self->{'socketi'}->read($buf,$cnt);
278 die "Server: Filesize $cnt: $num: $!\n" if not defined $num or $num<=0;
2eb6d82e
SV
279 print $fh $buf;
280 $res += $num;
a57a9493
MU
281 $cnt -= $num;
282 }
283 } elsif($line =~ s/^ //) {
2eb6d82e
SV
284 print $fh $line;
285 $res += length($line);
a57a9493
MU
286 } elsif($line =~ /^M\b/) {
287 # output, do nothing
288 } elsif($line =~ /^Mbinary\b/) {
289 my $cnt;
290 die "EOF from server after 'Mbinary'" unless defined ($cnt = $self->readline());
291 chomp $cnt;
292 die "Duh: Mbinary $cnt" if $cnt !~ /^\d+$/ or $cnt<1;
293 $line="";
294 while($cnt) {
295 my $buf;
296 my $num = $self->{'socketi'}->read($buf,$cnt);
297 die "S: Mbinary $cnt: $num: $!\n" if not defined $num or $num<=0;
2eb6d82e
SV
298 print $fh $buf;
299 $res += $num;
a57a9493
MU
300 $cnt -= $num;
301 }
302 } else {
303 chomp $line;
304 if($line eq "ok") {
305 # print STDERR "S: ok (".length($res).")\n";
306 return $res;
307 } elsif($line =~ s/^E //) {
308 # print STDERR "S: $line\n";
8b8840e0
MU
309 } elsif($line =~ /^Remove-entry /i) {
310 $line = $self->readline(); # filename
311 $line = $self->readline(); # OK
312 chomp $line;
313 die "Unknown: $line" if $line ne "ok";
314 return -1;
a57a9493
MU
315 } else {
316 die "Unknown: $line\n";
317 }
318 }
319 }
320}
321sub file {
322 my($self,$fn,$rev) = @_;
323 my $res;
324
2eb6d82e
SV
325 my ($fh, $name) = tempfile('gitcvs.XXXXXX',
326 DIR => File::Spec->tmpdir(), UNLINK => 1);
327
328 $self->_file($fn,$rev) and $res = $self->_line($fh);
329
330 if (!defined $res) {
331 # retry
332 $self->conn();
333 $self->_file($fn,$rev)
334 or die "No file command send\n";
335 $res = $self->_line($fh);
336 die "No input: $fn $rev\n" unless defined $res;
a57a9493 337 }
c619ad51 338 close ($fh);
a57a9493 339
d44e8cf0
ML
340 if ($res eq '') {
341 die "Looks like the server has gone away while fetching $fn $rev -- exiting!";
342 }
343
2eb6d82e 344 return ($name, $res);
a57a9493
MU
345}
346
347
348package main;
349
2a3e1a85 350my $cvs = CVSconn->new($opt_d, $cvs_tree);
a57a9493
MU
351
352
353sub pdate($) {
354 my($d) = @_;
355 m#(\d{2,4})/(\d\d)/(\d\d)\s(\d\d):(\d\d)(?::(\d\d))?#
356 or die "Unparseable date: $d\n";
357 my $y=$1; $y-=1900 if $y>1900;
358 return timegm($6||0,$5,$4,$3,$2-1,$y);
9718a00b
TM
359}
360
a57a9493
MU
361sub pmode($) {
362 my($mode) = @_;
363 my $m = 0;
364 my $mm = 0;
365 my $um = 0;
366 for my $x(split(//,$mode)) {
367 if($x eq ",") {
368 $m |= $mm&$um;
369 $mm = 0;
370 $um = 0;
371 } elsif($x eq "u") { $um |= 0700;
372 } elsif($x eq "g") { $um |= 0070;
373 } elsif($x eq "o") { $um |= 0007;
374 } elsif($x eq "r") { $mm |= 0444;
375 } elsif($x eq "w") { $mm |= 0222;
376 } elsif($x eq "x") { $mm |= 0111;
377 } elsif($x eq "=") { # do nothing
378 } else { die "Unknown mode: $mode\n";
379 }
380 }
381 $m |= $mm&$um;
382 return $m;
383}
d4f8b390 384
a57a9493
MU
385sub getwd() {
386 my $pwd = `pwd`;
387 chomp $pwd;
388 return $pwd;
d4f8b390
LT
389}
390
db4b6582
ML
391
392sub get_headref($$) {
393 my $name = shift;
394 my $git_dir = shift;
395 my $sha;
396
397 if (open(C,"$git_dir/refs/heads/$name")) {
398 chomp($sha = <C>);
399 close(C);
400 length($sha) == 40
401 or die "Cannot get head id for $name ($sha): $!\n";
402 }
403 return $sha;
404}
405
406
a57a9493
MU
407-d $git_tree
408 or mkdir($git_tree,0777)
409 or die "Could not create $git_tree: $!";
410chdir($git_tree);
d4f8b390 411
a57a9493 412my $last_branch = "";
46541669 413my $orig_branch = "";
210569f9 414my $forward_master = 0;
a57a9493
MU
415my %branch_date;
416
417my $git_dir = $ENV{"GIT_DIR"} || ".git";
418$git_dir = getwd()."/".$git_dir unless $git_dir =~ m#^/#;
419$ENV{"GIT_DIR"} = $git_dir;
79ee456c
SV
420my $orig_git_index;
421$orig_git_index = $ENV{GIT_INDEX_FILE} if exists $ENV{GIT_INDEX_FILE};
422my ($git_ih, $git_index) = tempfile('gitXXXXXX', SUFFIX => '.idx',
423 DIR => File::Spec->tmpdir());
424close ($git_ih);
425$ENV{GIT_INDEX_FILE} = $git_index;
a57a9493
MU
426unless(-d $git_dir) {
427 system("git-init-db");
428 die "Cannot init the GIT db at $git_tree: $?\n" if $?;
429 system("git-read-tree");
430 die "Cannot init an empty tree: $?\n" if $?;
431
432 $last_branch = $opt_o;
46541669 433 $orig_branch = "";
a57a9493 434} else {
866d1310 435 -f "$git_dir/refs/heads/$opt_o"
4c24e089
MU
436 or die "Branch '$opt_o' does not exist.\n".
437 "Either use the correct '-o branch' option,\n".
438 "or import to a new repository.\n";
439
a57a9493 440 $last_branch = basename(readlink("$git_dir/HEAD"));
46541669
MU
441 unless($last_branch) {
442 warn "Cannot read the last branch name: $! -- assuming 'master'\n";
443 $last_branch = "master";
444 }
445 $orig_branch = $last_branch;
210569f9
SV
446 if (-f "$git_dir/CVS2GIT_HEAD") {
447 die <<EOM;
448CVS2GIT_HEAD exists.
449Make sure your working directory corresponds to HEAD and remove CVS2GIT_HEAD.
450You may need to run
451
452 git-read-tree -m -u CVS2GIT_HEAD HEAD
453EOM
454 }
455 system('cp', "$git_dir/HEAD", "$git_dir/CVS2GIT_HEAD");
456
457 $forward_master =
458 $opt_o ne 'master' && -f "$git_dir/refs/heads/master" &&
459 system('cmp', '-s', "$git_dir/refs/heads/master",
460 "$git_dir/refs/heads/$opt_o") == 0;
a57a9493 461
79ee456c
SV
462 # populate index
463 system('git-read-tree', $last_branch);
17501132 464 die "read-tree failed: $?\n" if $?;
a57a9493
MU
465
466 # Get the last import timestamps
467 opendir(D,"$git_dir/refs/heads");
468 while(defined(my $head = readdir(D))) {
469 next if $head =~ /^\./;
470 open(F,"$git_dir/refs/heads/$head")
471 or die "Bad head branch: $head: $!\n";
472 chomp(my $ftag = <F>);
473 close(F);
474 open(F,"git-cat-file commit $ftag |");
475 while(<F>) {
476 next unless /^author\s.*\s(\d+)\s[-+]\d{4}$/;
477 $branch_date{$head} = $1;
478 last;
479 }
480 close(F);
481 }
482 closedir(D);
483}
484
485-d $git_dir
486 or die "Could not create git subdir ($git_dir).\n";
487
488my $pid = open(CVS,"-|");
489die "Cannot fork: $!\n" unless defined $pid;
490unless($pid) {
2be4fcc3
MU
491 my @opt;
492 @opt = split(/,/,$opt_p) if defined $opt_p;
28537171 493 unshift @opt, '-z', $opt_z if defined $opt_z;
e1757689 494 unless (defined($opt_p) && $opt_p =~ m/--no-cvs-direct/) {
df73e9c6
ML
495 push @opt, '--cvs-direct';
496 }
497 exec("cvsps",@opt,"-u","-A",'--root',$opt_d,$cvs_tree);
a57a9493
MU
498 die "Could not start cvsps: $!\n";
499}
500
501
502## cvsps output:
503#---------------------
504#PatchSet 314
505#Date: 1999/09/18 13:03:59
506#Author: wkoch
507#Branch: STABLE-BRANCH-1-0
508#Ancestor branch: HEAD
509#Tag: (none)
510#Log:
511# See ChangeLog: Sat Sep 18 13:03:28 CEST 1999 Werner Koch
512#Members:
513# README:1.57->1.57.2.1
514# VERSION:1.96->1.96.2.1
515#
516#---------------------
517
518my $state = 0;
519
94c23343 520my($patchset,$date,$author_name,$author_email,$branch,$ancestor,$tag,$logmsg);
a57a9493
MU
521my(@old,@new);
522my $commit = sub {
523 my $pid;
4abdecbf
MU
524 while(@old) {
525 my @o2;
526 if(@old > 55) {
527 @o2 = splice(@old,0,50);
528 } else {
529 @o2 = @old;
530 @old = ();
531 }
215a7ad1 532 system("git-update-index","--force-remove","--",@o2);
4abdecbf
MU
533 die "Cannot remove files: $?\n" if $?;
534 }
535 while(@new) {
536 my @n2;
2eb6d82e
SV
537 if(@new > 12) {
538 @n2 = splice(@new,0,10);
4abdecbf
MU
539 } else {
540 @n2 = @new;
541 @new = ();
542 }
215a7ad1 543 system("git-update-index","--add",
2eb6d82e 544 (map { ('--cacheinfo', @$_) } @n2));
4abdecbf
MU
545 die "Cannot add files: $?\n" if $?;
546 }
a57a9493
MU
547
548 $pid = open(C,"-|");
549 die "Cannot fork: $!" unless defined $pid;
550 unless($pid) {
551 exec("git-write-tree");
552 die "Cannot exec git-write-tree: $!\n";
553 }
554 chomp(my $tree = <C>);
555 length($tree) == 40
556 or die "Cannot get tree id ($tree): $!\n";
557 close(C)
558 or die "Error running git-write-tree: $?\n";
559 print "Tree ID $tree\n" if $opt_v;
560
561 my $parent = "";
562 if(open(C,"$git_dir/refs/heads/$last_branch")) {
563 chomp($parent = <C>);
564 close(C);
565 length($parent) == 40
566 or die "Cannot get parent id ($parent): $!\n";
567 print "Parent ID $parent\n" if $opt_v;
568 }
569
17501132
SV
570 my $pr = IO::Pipe->new() or die "Cannot open pipe: $!\n";
571 my $pw = IO::Pipe->new() or die "Cannot open pipe: $!\n";
2a3e1a85
MU
572 $pid = fork();
573 die "Fork: $!\n" unless defined $pid;
a57a9493 574 unless($pid) {
2a3e1a85
MU
575 $pr->writer();
576 $pw->reader();
89764f5d 577 open(OUT,">&STDOUT");
2a3e1a85
MU
578 dup2($pw->fileno(),0);
579 dup2($pr->fileno(),1);
580 $pr->close();
581 $pw->close();
582
a57a9493
MU
583 my @par = ();
584 @par = ("-p",$parent) if $parent;
db4b6582
ML
585
586 # loose detection of merges
587 # based on the commit msg
588 foreach my $rx (@mergerx) {
589 if ($logmsg =~ $rx) {
590 my $mparent = $1;
049f28c3 591 if ($mparent eq 'HEAD') { $mparent = $opt_o };
db4b6582
ML
592 if ( -e "$git_dir/refs/heads/$mparent") {
593 $mparent = get_headref($mparent, $git_dir);
594 push @par, '-p', $mparent;
89764f5d 595 print OUT "Merge parent branch: $mparent\n" if $opt_v;
db4b6582 596 }
29504118 597 }
db4b6582
ML
598 }
599
a57a9493 600 exec("env",
94c23343
JH
601 "GIT_AUTHOR_NAME=$author_name",
602 "GIT_AUTHOR_EMAIL=$author_email",
a57a9493 603 "GIT_AUTHOR_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
94c23343
JH
604 "GIT_COMMITTER_NAME=$author_name",
605 "GIT_COMMITTER_EMAIL=$author_email",
a57a9493
MU
606 "GIT_COMMITTER_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
607 "git-commit-tree", $tree,@par);
608 die "Cannot exec git-commit-tree: $!\n";
609 }
2a3e1a85
MU
610 $pw->writer();
611 $pr->reader();
e371046b
MU
612
613 # compatibility with git2cvs
614 substr($logmsg,32767) = "" if length($logmsg) > 32767;
615 $logmsg =~ s/[\s\n]+\z//;
616
617 print $pw "$logmsg\n"
a57a9493 618 or die "Error writing to git-commit-tree: $!\n";
2a3e1a85
MU
619 $pw->close();
620
8b8840e0 621 print "Committed patch $patchset ($branch ".strftime("%Y-%m-%d %H:%M:%S",gmtime($date)).")\n" if $opt_v;
2a3e1a85 622 chomp(my $cid = <$pr>);
a57a9493
MU
623 length($cid) == 40
624 or die "Cannot get commit id ($cid): $!\n";
625 print "Commit ID $cid\n" if $opt_v;
2a3e1a85
MU
626 $pr->close();
627
628 waitpid($pid,0);
629 die "Error running git-commit-tree: $?\n" if $?;
a57a9493
MU
630
631 open(C,">$git_dir/refs/heads/$branch")
632 or die "Cannot open branch $branch for update: $!\n";
633 print C "$cid\n"
634 or die "Cannot write branch $branch for update: $!\n";
635 close(C)
636 or die "Cannot write branch $branch for update: $!\n";
637
638 if($tag) {
0d821d4d
PA
639 my($in, $out) = ('','');
640 my($xtag) = $tag;
641 $xtag =~ s/\s+\*\*.*$//; # Remove stuff like ** INVALID ** and ** FUNKY **
642 $xtag =~ tr/_/\./ if ( $opt_u );
643
644 my $pid = open2($in, $out, 'git-mktag');
645 print $out "object $cid\n".
646 "type commit\n".
647 "tag $xtag\n".
94c23343 648 "tagger $author_name <$author_email>\n"
0d821d4d
PA
649 or die "Cannot create tag object $xtag: $!\n";
650 close($out)
651 or die "Cannot create tag object $xtag: $!\n";
652
653 my $tagobj = <$in>;
654 chomp $tagobj;
655
656 if ( !close($in) or waitpid($pid, 0) != $pid or
657 $? != 0 or $tagobj !~ /^[0123456789abcdef]{40}$/ ) {
658 die "Cannot create tag object $xtag: $!\n";
659 }
660
661
662 open(C,">$git_dir/refs/tags/$xtag")
663 or die "Cannot create tag $xtag: $!\n";
664 print C "$tagobj\n"
665 or die "Cannot write tag $xtag: $!\n";
a57a9493 666 close(C)
0d821d4d
PA
667 or die "Cannot write tag $xtag: $!\n";
668
669 print "Created tag '$xtag' on '$branch'\n" if $opt_v;
a57a9493 670 }
a57a9493
MU
671};
672
673while(<CVS>) {
674 chomp;
675 if($state == 0 and /^-+$/) {
676 $state = 1;
677 } elsif($state == 0) {
678 $state = 1;
679 redo;
680 } elsif(($state==0 or $state==1) and s/^PatchSet\s+//) {
681 $patchset = 0+$_;
682 $state=2;
683 } elsif($state == 2 and s/^Date:\s+//) {
684 $date = pdate($_);
685 unless($date) {
686 print STDERR "Could not parse date: $_\n";
687 $state=0;
688 next;
689 }
690 $state=3;
691 } elsif($state == 3 and s/^Author:\s+//) {
692 s/\s+$//;
94c23343
JH
693 if (/^(.*?)\s+<(.*)>/) {
694 ($author_name, $author_email) = ($1, $2);
695 } else {
696 $author_name = $author_email = $_;
697 }
a57a9493
MU
698 $state = 4;
699 } elsif($state == 4 and s/^Branch:\s+//) {
700 s/\s+$//;
fbfd60d6 701 s/[\/]/$opt_s/g;
a57a9493
MU
702 $branch = $_;
703 $state = 5;
704 } elsif($state == 5 and s/^Ancestor branch:\s+//) {
705 s/\s+$//;
706 $ancestor = $_;
0fa2824f 707 $ancestor = $opt_o if $ancestor eq "HEAD";
a57a9493
MU
708 $state = 6;
709 } elsif($state == 5) {
710 $ancestor = undef;
711 $state = 6;
712 redo;
713 } elsif($state == 6 and s/^Tag:\s+//) {
714 s/\s+$//;
715 if($_ eq "(none)") {
716 $tag = undef;
717 } else {
718 $tag = $_;
719 }
720 $state = 7;
721 } elsif($state == 7 and /^Log:/) {
722 $logmsg = "";
723 $state = 8;
724 } elsif($state == 8 and /^Members:/) {
725 $branch = $opt_o if $branch eq "HEAD";
726 if(defined $branch_date{$branch} and $branch_date{$branch} >= $date) {
727 # skip
9da07f34 728 print "skip patchset $patchset: $date before $branch_date{$branch}\n" if $opt_v;
a57a9493
MU
729 $state = 11;
730 next;
731 }
732 if($ancestor) {
733 if(-f "$git_dir/refs/heads/$branch") {
734 print STDERR "Branch $branch already exists!\n";
735 $state=11;
736 next;
737 }
738 unless(open(H,"$git_dir/refs/heads/$ancestor")) {
739 print STDERR "Branch $ancestor does not exist!\n";
740 $state=11;
741 next;
742 }
743 chomp(my $id = <H>);
744 close(H);
745 unless(open(H,"> $git_dir/refs/heads/$branch")) {
746 print STDERR "Could not create branch $branch: $!\n";
747 $state=11;
748 next;
749 }
750 print H "$id\n"
751 or die "Could not write branch $branch: $!";
752 close(H)
753 or die "Could not write branch $branch: $!";
754 }
755 if(($ancestor || $branch) ne $last_branch) {
2a3e1a85 756 print "Switching from $last_branch to $branch\n" if $opt_v;
46e63efc 757 system("git-read-tree", $branch);
46541669 758 die "read-tree failed: $?\n" if $?;
a57a9493 759 }
46e63efc 760 $last_branch = $branch if $branch ne $last_branch;
a57a9493
MU
761 $state = 9;
762 } elsif($state == 8) {
763 $logmsg .= "$_\n";
8b8840e0 764 } elsif($state == 9 and /^\s+(.+?):(INITIAL|\d+(?:\.\d+)+)->(\d+(?:\.\d+)+)\s*$/) {
a57a9493 765# VERSION:1.96->1.96.2.1
2a3e1a85 766 my $init = ($2 eq "INITIAL");
a57a9493 767 my $fn = $1;
f65ae603
MU
768 my $rev = $3;
769 $fn =~ s#^/+##;
2eb6d82e 770 my ($tmpname, $size) = $cvs->file($fn,$rev);
8b8840e0
MU
771 if($size == -1) {
772 push(@old,$fn);
773 print "Drop $fn\n" if $opt_v;
774 } else {
775 print "".($init ? "New" : "Update")." $fn: $size bytes\n" if $opt_v;
776 open my $F, '-|', "git-hash-object -w $tmpname"
777 or die "Cannot create object: $!\n";
778 my $sha = <$F>;
779 chomp $sha;
780 close $F;
781 my $mode = pmode($cvs->{'mode'});
782 push(@new,[$mode, $sha, $fn]); # may be resurrected!
783 }
2eb6d82e 784 unlink($tmpname);
b0921331 785 } elsif($state == 9 and /^\s+(.+?):\d+(?:\.\d+)+->(\d+(?:\.\d+)+)\(DEAD\)\s*$/) {
f65ae603
MU
786 my $fn = $1;
787 $fn =~ s#^/+##;
788 push(@old,$fn);
8b8840e0 789 print "Delete $fn\n" if $opt_v;
a57a9493
MU
790 } elsif($state == 9 and /^\s*$/) {
791 $state = 10;
792 } elsif(($state == 9 or $state == 10) and /^-+$/) {
793 &$commit();
794 $state = 1;
795 } elsif($state == 11 and /^-+$/) {
796 $state = 1;
797 } elsif(/^-+$/) { # end of unknown-line processing
798 $state = 1;
799 } elsif($state != 11) { # ignore stuff when skipping
800 print "* UNKNOWN LINE * $_\n";
801 }
802}
803&$commit() if $branch and $state != 11;
d4f8b390 804
79ee456c
SV
805unlink($git_index);
806
210569f9
SV
807if (defined $orig_git_index) {
808 $ENV{GIT_INDEX_FILE} = $orig_git_index;
809} else {
810 delete $ENV{GIT_INDEX_FILE};
811}
812
46541669
MU
813# Now switch back to the branch we were in before all of this happened
814if($orig_branch) {
79ee456c 815 print "DONE\n" if $opt_v;
210569f9
SV
816 system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
817 if $forward_master;
c1c774e7
SV
818 unless ($opt_i) {
819 system('git-read-tree', '-m', '-u', 'CVS2GIT_HEAD', 'HEAD');
820 die "read-tree failed: $?\n" if $?;
821 }
46541669
MU
822} else {
823 $orig_branch = "master";
824 print "DONE; creating $orig_branch branch\n" if $opt_v;
825 system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
826 unless -f "$git_dir/refs/heads/master";
79ee456c
SV
827 unlink("$git_dir/HEAD");
828 symlink("refs/heads/$orig_branch","$git_dir/HEAD");
c1c774e7
SV
829 unless ($opt_i) {
830 system('git checkout');
831 die "checkout failed: $?\n" if $?;
832 }
46541669 833}
210569f9 834unlink("$git_dir/CVS2GIT_HEAD");