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