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