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