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