git-svn: document --username
[git/platforms.git] / git-cvsimport.perl
blob1a1ba7b1a6f779773702ae3c1efb56425c31beb4
1 #!/usr/bin/perl -w
3 # This tool is copyright (c) 2005, Matthias Urlichs.
4 # It is released under the Gnu Public License, version 2.
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.
10 # Checking out the files is done by a single long-running CVS connection
11 # / server process.
13 # The head revision is on branch "origin" by default.
14 # You can change that with the '-o' option.
16 use strict;
17 use warnings;
18 use Getopt::Std;
19 use File::Spec;
20 use File::Temp qw(tempfile tmpnam);
21 use File::Path qw(mkpath);
22 use File::Basename qw(basename dirname);
23 use Time::Local;
24 use IO::Socket;
25 use IO::Pipe;
26 use POSIX qw(strftime dup2 ENOENT);
27 use IPC::Open2;
29 $SIG{'PIPE'}="IGNORE";
30 $ENV{'TZ'}="UTC";
32 our ($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, $opt_a);
33 my (%conv_author_name, %conv_author_email);
35 sub usage() {
36 print STDERR <<END;
37 Usage: ${\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] [-a] [-m] [-M regex] [-S regex] [CVS_module]
41 END
42 exit(1);
45 sub read_author_info($) {
46 my ($file) = @_;
47 my $user;
48 open my $f, '<', "$file" or die("Failed to open $file: $!\n");
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;
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;
67 elsif ($mapped =~ /^<?(.*)>?$/) {
68 $conv_author_name{$user} = $user;
69 $conv_author_email{$user} = $1;
72 # NEEDSWORK: Maybe warn on unrecognized lines?
74 close ($f);
77 sub write_author_info($) {
78 my ($file) = @_;
79 open my $f, '>', $file or
80 die("Failed to open $file for writing: $!");
82 foreach (keys %conv_author_name) {
83 print $f "$_=$conv_author_name{$_} <$conv_author_email{$_}>\n";
85 close ($f);
88 # convert getopts specs for use by git-repo-config
89 sub read_repo_config {
90 # Split the string between characters, unless there is a ':'
91 # So "abc:de" becomes ["a", "b", "c:", "d", "e"]
92 my @opts = split(/ *(?!:)/, shift);
93 foreach my $o (@opts) {
94 my $key = $o;
95 $key =~ s/://g;
96 my $arg = 'git-repo-config';
97 $arg .= ' --bool' if ($o !~ /:$/);
99 chomp(my $tmp = `$arg --get cvsimport.$key`);
100 if ($tmp && !($arg =~ /--bool/ && $tmp eq 'false')) {
101 no strict 'refs';
102 my $opt_name = "opt_" . $key;
103 if (!$$opt_name) {
104 $$opt_name = $tmp;
108 if (@ARGV == 0) {
109 chomp(my $module = `git-repo-config --get cvsimport.module`);
110 push(@ARGV, $module);
114 my $opts = "haivmkuo:d:p:C:z:s:M:P:A:S:L:";
115 read_repo_config($opts);
116 getopts($opts) or usage();
117 usage if $opt_h;
119 @ARGV <= 1 or usage();
121 if ($opt_d) {
122 $ENV{"CVSROOT"} = $opt_d;
123 } elsif (-f 'CVS/Root') {
124 open my $f, '<', 'CVS/Root' or die 'Failed to open CVS/Root';
125 $opt_d = <$f>;
126 chomp $opt_d;
127 close $f;
128 $ENV{"CVSROOT"} = $opt_d;
129 } elsif ($ENV{"CVSROOT"}) {
130 $opt_d = $ENV{"CVSROOT"};
131 } else {
132 die "CVSROOT needs to be set";
134 $opt_o ||= "origin";
135 $opt_s ||= "-";
136 $opt_a ||= 0;
138 my $git_tree = $opt_C;
139 $git_tree ||= ".";
141 my $cvs_tree;
142 if ($#ARGV == 0) {
143 $cvs_tree = $ARGV[0];
144 } elsif (-f 'CVS/Repository') {
145 open my $f, '<', 'CVS/Repository' or
146 die 'Failed to open CVS/Repository';
147 $cvs_tree = <$f>;
148 chomp $cvs_tree;
149 close $f;
150 } else {
151 usage();
154 our @mergerx = ();
155 if ($opt_m) {
156 @mergerx = ( qr/\W(?:from|of|merge|merging|merged) (\w+)/i );
158 if ($opt_M) {
159 push (@mergerx, qr/$opt_M/);
162 # Remember UTC of our starting time
163 # we'll want to avoid importing commits
164 # that are too recent
165 our $starttime = time();
167 select(STDERR); $|=1; select(STDOUT);
170 package CVSconn;
171 # Basic CVS dialog.
172 # We're only interested in connecting and downloading, so ...
174 use File::Spec;
175 use File::Temp qw(tempfile);
176 use POSIX qw(strftime dup2);
178 sub new {
179 my ($what,$repo,$subdir) = @_;
180 $what=ref($what) if ref($what);
182 my $self = {};
183 $self->{'buffer'} = "";
184 bless($self,$what);
186 $repo =~ s#/+$##;
187 $self->{'fullrep'} = $repo;
188 $self->conn();
190 $self->{'subdir'} = $subdir;
191 $self->{'lines'} = undef;
193 return $self;
196 sub conn {
197 my $self = shift;
198 my $repo = $self->{'fullrep'};
199 if ($repo =~ s/^:pserver(?:([^:]*)):(?:(.*?)(?::(.*?))?@)?([^:\/]*)(?::(\d*))?//) {
200 my ($param,$user,$pass,$serv,$port) = ($1,$2,$3,$4,$5);
202 my ($proxyhost,$proxyport);
203 if ($param && ($param =~ m/proxy=([^;]+)/)) {
204 $proxyhost = $1;
205 # Default proxyport, if not specified, is 8080.
206 $proxyport = 8080;
207 if ($ENV{"CVS_PROXY_PORT"}) {
208 $proxyport = $ENV{"CVS_PROXY_PORT"};
210 if ($param =~ m/proxyport=([^;]+)/) {
211 $proxyport = $1;
215 $user="anonymous" unless defined $user;
216 my $rr2 = "-";
217 unless ($port) {
218 $rr2 = ":pserver:$user\@$serv:$repo";
219 $port=2401;
221 my $rr = ":pserver:$user\@$serv:$port$repo";
223 unless ($pass) {
224 open(H,$ENV{'HOME'}."/.cvspass") and do {
225 # :pserver:cvs@mea.tmt.tele.fi:/cvsroot/zmailer Ah<Z
226 while (<H>) {
227 chomp;
228 s/^\/\d+\s+//;
229 my ($w,$p) = split(/\s/,$_,2);
230 if ($w eq $rr or $w eq $rr2) {
231 $pass = $p;
232 last;
237 $pass="A" unless $pass;
239 my ($s, $rep);
240 if ($proxyhost) {
242 # Use a HTTP Proxy. Only works for HTTP proxies that
243 # don't require user authentication
245 # See: http://www.ietf.org/rfc/rfc2817.txt
247 $s = IO::Socket::INET->new(PeerHost => $proxyhost, PeerPort => $proxyport);
248 die "Socket to $proxyhost: $!\n" unless defined $s;
249 $s->write("CONNECT $serv:$port HTTP/1.1\r\nHost: $serv:$port\r\n\r\n")
250 or die "Write to $proxyhost: $!\n";
251 $s->flush();
253 $rep = <$s>;
255 # The answer should look like 'HTTP/1.x 2yy ....'
256 if (!($rep =~ m#^HTTP/1\.. 2[0-9][0-9]#)) {
257 die "Proxy connect: $rep\n";
259 # Skip up to the empty line of the proxy server output
260 # including the response headers.
261 while ($rep = <$s>) {
262 last if (!defined $rep ||
263 $rep eq "\n" ||
264 $rep eq "\r\n");
266 } else {
267 $s = IO::Socket::INET->new(PeerHost => $serv, PeerPort => $port);
268 die "Socket to $serv: $!\n" unless defined $s;
271 $s->write("BEGIN AUTH REQUEST\n$repo\n$user\n$pass\nEND AUTH REQUEST\n")
272 or die "Write to $serv: $!\n";
273 $s->flush();
275 $rep = <$s>;
277 if ($rep ne "I LOVE YOU\n") {
278 $rep="<unknown>" unless $rep;
279 die "AuthReply: $rep\n";
281 $self->{'socketo'} = $s;
282 $self->{'socketi'} = $s;
283 } else { # local or ext: Fork off our own cvs server.
284 my $pr = IO::Pipe->new();
285 my $pw = IO::Pipe->new();
286 my $pid = fork();
287 die "Fork: $!\n" unless defined $pid;
288 my $cvs = 'cvs';
289 $cvs = $ENV{CVS_SERVER} if exists $ENV{CVS_SERVER};
290 my $rsh = 'rsh';
291 $rsh = $ENV{CVS_RSH} if exists $ENV{CVS_RSH};
293 my @cvs = ($cvs, 'server');
294 my ($local, $user, $host);
295 $local = $repo =~ s/:local://;
296 if (!$local) {
297 $repo =~ s/:ext://;
298 $local = !($repo =~ s/^(?:([^\@:]+)\@)?([^:]+)://);
299 ($user, $host) = ($1, $2);
301 if (!$local) {
302 if ($user) {
303 unshift @cvs, $rsh, '-l', $user, $host;
304 } else {
305 unshift @cvs, $rsh, $host;
309 unless ($pid) {
310 $pr->writer();
311 $pw->reader();
312 dup2($pw->fileno(),0);
313 dup2($pr->fileno(),1);
314 $pr->close();
315 $pw->close();
316 exec(@cvs);
318 $pw->writer();
319 $pr->reader();
320 $self->{'socketo'} = $pw;
321 $self->{'socketi'} = $pr;
323 $self->{'socketo'}->write("Root $repo\n");
325 # Trial and error says that this probably is the minimum set
326 $self->{'socketo'}->write("Valid-responses ok error Valid-requests Mode M Mbinary E Checked-in Created Updated Merged Removed\n");
328 $self->{'socketo'}->write("valid-requests\n");
329 $self->{'socketo'}->flush();
331 chomp(my $rep=$self->readline());
332 if ($rep !~ s/^Valid-requests\s*//) {
333 $rep="<unknown>" unless $rep;
334 die "Expected Valid-requests from server, but got: $rep\n";
336 chomp(my $res=$self->readline());
337 die "validReply: $res\n" if $res ne "ok";
339 $self->{'socketo'}->write("UseUnchanged\n") if $rep =~ /\bUseUnchanged\b/;
340 $self->{'repo'} = $repo;
343 sub readline {
344 my ($self) = @_;
345 return $self->{'socketi'}->getline();
348 sub _file {
349 # Request a file with a given revision.
350 # Trial and error says this is a good way to do it. :-/
351 my ($self,$fn,$rev) = @_;
352 $self->{'socketo'}->write("Argument -N\n") or return undef;
353 $self->{'socketo'}->write("Argument -P\n") or return undef;
354 # -kk: Linus' version doesn't use it - defaults to off
355 if ($opt_k) {
356 $self->{'socketo'}->write("Argument -kk\n") or return undef;
358 $self->{'socketo'}->write("Argument -r\n") or return undef;
359 $self->{'socketo'}->write("Argument $rev\n") or return undef;
360 $self->{'socketo'}->write("Argument --\n") or return undef;
361 $self->{'socketo'}->write("Argument $self->{'subdir'}/$fn\n") or return undef;
362 $self->{'socketo'}->write("Directory .\n") or return undef;
363 $self->{'socketo'}->write("$self->{'repo'}\n") or return undef;
364 # $self->{'socketo'}->write("Sticky T1.0\n") or return undef;
365 $self->{'socketo'}->write("co\n") or return undef;
366 $self->{'socketo'}->flush() or return undef;
367 $self->{'lines'} = 0;
368 return 1;
370 sub _line {
371 # Read a line from the server.
372 # ... except that 'line' may be an entire file. ;-)
373 my ($self, $fh) = @_;
374 die "Not in lines" unless defined $self->{'lines'};
376 my $line;
377 my $res=0;
378 while (defined($line = $self->readline())) {
379 # M U gnupg-cvs-rep/AUTHORS
380 # Updated gnupg-cvs-rep/
381 # /daten/src/rsync/gnupg-cvs-rep/AUTHORS
382 # /AUTHORS/1.1///T1.1
383 # u=rw,g=rw,o=rw
385 # ok
387 if ($line =~ s/^(?:Created|Updated) //) {
388 $line = $self->readline(); # path
389 $line = $self->readline(); # Entries line
390 my $mode = $self->readline(); chomp $mode;
391 $self->{'mode'} = $mode;
392 defined (my $cnt = $self->readline())
393 or die "EOF from server after 'Changed'\n";
394 chomp $cnt;
395 die "Duh: Filesize $cnt" if $cnt !~ /^\d+$/;
396 $line="";
397 $res = $self->_fetchfile($fh, $cnt);
398 } elsif ($line =~ s/^ //) {
399 print $fh $line;
400 $res += length($line);
401 } elsif ($line =~ /^M\b/) {
402 # output, do nothing
403 } elsif ($line =~ /^Mbinary\b/) {
404 my $cnt;
405 die "EOF from server after 'Mbinary'" unless defined ($cnt = $self->readline());
406 chomp $cnt;
407 die "Duh: Mbinary $cnt" if $cnt !~ /^\d+$/ or $cnt<1;
408 $line="";
409 $res += $self->_fetchfile($fh, $cnt);
410 } else {
411 chomp $line;
412 if ($line eq "ok") {
413 # print STDERR "S: ok (".length($res).")\n";
414 return $res;
415 } elsif ($line =~ s/^E //) {
416 # print STDERR "S: $line\n";
417 } elsif ($line =~ /^(Remove-entry|Removed) /i) {
418 $line = $self->readline(); # filename
419 $line = $self->readline(); # OK
420 chomp $line;
421 die "Unknown: $line" if $line ne "ok";
422 return -1;
423 } else {
424 die "Unknown: $line\n";
428 return undef;
430 sub file {
431 my ($self,$fn,$rev) = @_;
432 my $res;
434 my ($fh, $name) = tempfile('gitcvs.XXXXXX',
435 DIR => File::Spec->tmpdir(), UNLINK => 1);
437 $self->_file($fn,$rev) and $res = $self->_line($fh);
439 if (!defined $res) {
440 print STDERR "Server has gone away while fetching $fn $rev, retrying...\n";
441 truncate $fh, 0;
442 $self->conn();
443 $self->_file($fn,$rev) or die "No file command send";
444 $res = $self->_line($fh);
445 die "Retry failed" unless defined $res;
447 close ($fh);
449 return ($name, $res);
451 sub _fetchfile {
452 my ($self, $fh, $cnt) = @_;
453 my $res = 0;
454 my $bufsize = 1024 * 1024;
455 while ($cnt) {
456 if ($bufsize > $cnt) {
457 $bufsize = $cnt;
459 my $buf;
460 my $num = $self->{'socketi'}->read($buf,$bufsize);
461 die "Server: Filesize $cnt: $num: $!\n" if not defined $num or $num<=0;
462 print $fh $buf;
463 $res += $num;
464 $cnt -= $num;
466 return $res;
470 package main;
472 my $cvs = CVSconn->new($opt_d, $cvs_tree);
475 sub pdate($) {
476 my ($d) = @_;
477 m#(\d{2,4})/(\d\d)/(\d\d)\s(\d\d):(\d\d)(?::(\d\d))?#
478 or die "Unparseable date: $d\n";
479 my $y=$1; $y-=1900 if $y>1900;
480 return timegm($6||0,$5,$4,$3,$2-1,$y);
483 sub pmode($) {
484 my ($mode) = @_;
485 my $m = 0;
486 my $mm = 0;
487 my $um = 0;
488 for my $x(split(//,$mode)) {
489 if ($x eq ",") {
490 $m |= $mm&$um;
491 $mm = 0;
492 $um = 0;
493 } elsif ($x eq "u") { $um |= 0700;
494 } elsif ($x eq "g") { $um |= 0070;
495 } elsif ($x eq "o") { $um |= 0007;
496 } elsif ($x eq "r") { $mm |= 0444;
497 } elsif ($x eq "w") { $mm |= 0222;
498 } elsif ($x eq "x") { $mm |= 0111;
499 } elsif ($x eq "=") { # do nothing
500 } else { die "Unknown mode: $mode\n";
503 $m |= $mm&$um;
504 return $m;
507 sub getwd() {
508 my $pwd = `pwd`;
509 chomp $pwd;
510 return $pwd;
513 sub is_sha1 {
514 my $s = shift;
515 return $s =~ /^[a-f0-9]{40}$/;
518 sub get_headref ($$) {
519 my $name = shift;
520 my $git_dir = shift;
522 my $f = "$git_dir/refs/heads/$name";
523 if (open(my $fh, $f)) {
524 chomp(my $r = <$fh>);
525 is_sha1($r) or die "Cannot get head id for $name ($r): $!";
526 return $r;
528 die "unable to open $f: $!" unless $! == POSIX::ENOENT;
529 return undef;
532 -d $git_tree
533 or mkdir($git_tree,0777)
534 or die "Could not create $git_tree: $!";
535 chdir($git_tree);
537 my $last_branch = "";
538 my $orig_branch = "";
539 my %branch_date;
540 my $tip_at_start = undef;
542 my $git_dir = $ENV{"GIT_DIR"} || ".git";
543 $git_dir = getwd()."/".$git_dir unless $git_dir =~ m#^/#;
544 $ENV{"GIT_DIR"} = $git_dir;
545 my $orig_git_index;
546 $orig_git_index = $ENV{GIT_INDEX_FILE} if exists $ENV{GIT_INDEX_FILE};
548 my %index; # holds filenames of one index per branch
550 unless (-d $git_dir) {
551 system("git-init");
552 die "Cannot init the GIT db at $git_tree: $?\n" if $?;
553 system("git-read-tree");
554 die "Cannot init an empty tree: $?\n" if $?;
556 $last_branch = $opt_o;
557 $orig_branch = "";
558 } else {
559 -f "$git_dir/refs/heads/$opt_o"
560 or die "Branch '$opt_o' does not exist.\n".
561 "Either use the correct '-o branch' option,\n".
562 "or import to a new repository.\n";
564 open(F, "git-symbolic-ref HEAD |") or
565 die "Cannot run git-symbolic-ref: $!\n";
566 chomp ($last_branch = <F>);
567 $last_branch = basename($last_branch);
568 close(F);
569 unless ($last_branch) {
570 warn "Cannot read the last branch name: $! -- assuming 'master'\n";
571 $last_branch = "master";
573 $orig_branch = $last_branch;
574 $tip_at_start = `git-rev-parse --verify HEAD`;
576 # Get the last import timestamps
577 my $fmt = '($ref, $author) = (%(refname), %(author));';
578 open(H, "git-for-each-ref --perl --format='$fmt' refs/heads |") or
579 die "Cannot run git-for-each-ref: $!\n";
580 while (defined(my $entry = <H>)) {
581 my ($ref, $author);
582 eval($entry) || die "cannot eval refs list: $@";
583 my ($head) = ($ref =~ m|^refs/heads/(.*)|);
584 $author =~ /^.*\s(\d+)\s[-+]\d{4}$/;
585 $branch_date{$head} = $1;
587 close(H);
590 -d $git_dir
591 or die "Could not create git subdir ($git_dir).\n";
593 # now we read (and possibly save) author-info as well
594 -f "$git_dir/cvs-authors" and
595 read_author_info("$git_dir/cvs-authors");
596 if ($opt_A) {
597 read_author_info($opt_A);
598 write_author_info("$git_dir/cvs-authors");
603 # run cvsps into a file unless we are getting
604 # it passed as a file via $opt_P
606 my $cvspsfile;
607 unless ($opt_P) {
608 print "Running cvsps...\n" if $opt_v;
609 my $pid = open(CVSPS,"-|");
610 my $cvspsfh;
611 die "Cannot fork: $!\n" unless defined $pid;
612 unless ($pid) {
613 my @opt;
614 @opt = split(/,/,$opt_p) if defined $opt_p;
615 unshift @opt, '-z', $opt_z if defined $opt_z;
616 unshift @opt, '-q' unless defined $opt_v;
617 unless (defined($opt_p) && $opt_p =~ m/--no-cvs-direct/) {
618 push @opt, '--cvs-direct';
620 exec("cvsps","--norc",@opt,"-u","-A",'--root',$opt_d,$cvs_tree);
621 die "Could not start cvsps: $!\n";
623 ($cvspsfh, $cvspsfile) = tempfile('gitXXXXXX', SUFFIX => '.cvsps',
624 DIR => File::Spec->tmpdir());
625 while (<CVSPS>) {
626 print $cvspsfh $_;
628 close CVSPS;
629 close $cvspsfh;
630 } else {
631 $cvspsfile = $opt_P;
634 open(CVS, "<$cvspsfile") or die $!;
636 ## cvsps output:
637 #---------------------
638 #PatchSet 314
639 #Date: 1999/09/18 13:03:59
640 #Author: wkoch
641 #Branch: STABLE-BRANCH-1-0
642 #Ancestor branch: HEAD
643 #Tag: (none)
644 #Log:
645 # See ChangeLog: Sat Sep 18 13:03:28 CEST 1999 Werner Koch
646 #Members:
647 # README:1.57->1.57.2.1
648 # VERSION:1.96->1.96.2.1
650 #---------------------
652 my $state = 0;
654 sub update_index (\@\@) {
655 my $old = shift;
656 my $new = shift;
657 open(my $fh, '|-', qw(git-update-index -z --index-info))
658 or die "unable to open git-update-index: $!";
659 print $fh
660 (map { "0 0000000000000000000000000000000000000000\t$_\0" }
661 @$old),
662 (map { '100' . sprintf('%o', $_->[0]) . " $_->[1]\t$_->[2]\0" }
663 @$new)
664 or die "unable to write to git-update-index: $!";
665 close $fh
666 or die "unable to write to git-update-index: $!";
667 $? and die "git-update-index reported error: $?";
670 sub write_tree () {
671 open(my $fh, '-|', qw(git-write-tree))
672 or die "unable to open git-write-tree: $!";
673 chomp(my $tree = <$fh>);
674 is_sha1($tree)
675 or die "Cannot get tree id ($tree): $!";
676 close($fh)
677 or die "Error running git-write-tree: $?\n";
678 print "Tree ID $tree\n" if $opt_v;
679 return $tree;
682 my ($patchset,$date,$author_name,$author_email,$branch,$ancestor,$tag,$logmsg);
683 my (@old,@new,@skipped,%ignorebranch);
685 # commits that cvsps cannot place anywhere...
686 $ignorebranch{'#CVSPS_NO_BRANCH'} = 1;
688 sub commit {
689 if ($branch eq $opt_o && !$index{branch} && !get_headref($branch, $git_dir)) {
690 # looks like an initial commit
691 # use the index primed by git-init
692 $ENV{GIT_INDEX_FILE} = '.git/index';
693 $index{$branch} = '.git/index';
694 } else {
695 # use an index per branch to speed up
696 # imports of projects with many branches
697 unless ($index{$branch}) {
698 $index{$branch} = tmpnam();
699 $ENV{GIT_INDEX_FILE} = $index{$branch};
700 if ($ancestor) {
701 system("git-read-tree", $ancestor);
702 } else {
703 system("git-read-tree", $branch);
705 die "read-tree failed: $?\n" if $?;
708 $ENV{GIT_INDEX_FILE} = $index{$branch};
710 update_index(@old, @new);
711 @old = @new = ();
712 my $tree = write_tree();
713 my $parent = get_headref($last_branch, $git_dir);
714 print "Parent ID " . ($parent ? $parent : "(empty)") . "\n" if $opt_v;
716 my @commit_args;
717 push @commit_args, ("-p", $parent) if $parent;
719 # loose detection of merges
720 # based on the commit msg
721 foreach my $rx (@mergerx) {
722 next unless $logmsg =~ $rx && $1;
723 my $mparent = $1 eq 'HEAD' ? $opt_o : $1;
724 if (my $sha1 = get_headref($mparent, $git_dir)) {
725 push @commit_args, '-p', $mparent;
726 print "Merge parent branch: $mparent\n" if $opt_v;
730 my $commit_date = strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date));
731 $ENV{GIT_AUTHOR_NAME} = $author_name;
732 $ENV{GIT_AUTHOR_EMAIL} = $author_email;
733 $ENV{GIT_AUTHOR_DATE} = $commit_date;
734 $ENV{GIT_COMMITTER_NAME} = $author_name;
735 $ENV{GIT_COMMITTER_EMAIL} = $author_email;
736 $ENV{GIT_COMMITTER_DATE} = $commit_date;
737 my $pid = open2(my $commit_read, my $commit_write,
738 'git-commit-tree', $tree, @commit_args);
740 # compatibility with git2cvs
741 substr($logmsg,32767) = "" if length($logmsg) > 32767;
742 $logmsg =~ s/[\s\n]+\z//;
744 if (@skipped) {
745 $logmsg .= "\n\n\nSKIPPED:\n\t";
746 $logmsg .= join("\n\t", @skipped) . "\n";
747 @skipped = ();
750 print($commit_write "$logmsg\n") && close($commit_write)
751 or die "Error writing to git-commit-tree: $!\n";
753 print "Committed patch $patchset ($branch $commit_date)\n" if $opt_v;
754 chomp(my $cid = <$commit_read>);
755 is_sha1($cid) or die "Cannot get commit id ($cid): $!\n";
756 print "Commit ID $cid\n" if $opt_v;
757 close($commit_read);
759 waitpid($pid,0);
760 die "Error running git-commit-tree: $?\n" if $?;
762 system("git-update-ref refs/heads/$branch $cid") == 0
763 or die "Cannot write branch $branch for update: $!\n";
765 if ($tag) {
766 my ($in, $out) = ('','');
767 my ($xtag) = $tag;
768 $xtag =~ s/\s+\*\*.*$//; # Remove stuff like ** INVALID ** and ** FUNKY **
769 $xtag =~ tr/_/\./ if ( $opt_u );
770 $xtag =~ s/[\/]/$opt_s/g;
772 my $pid = open2($in, $out, 'git-mktag');
773 print $out "object $cid\n".
774 "type commit\n".
775 "tag $xtag\n".
776 "tagger $author_name <$author_email>\n"
777 or die "Cannot create tag object $xtag: $!\n";
778 close($out)
779 or die "Cannot create tag object $xtag: $!\n";
781 my $tagobj = <$in>;
782 chomp $tagobj;
784 if ( !close($in) or waitpid($pid, 0) != $pid or
785 $? != 0 or $tagobj !~ /^[0123456789abcdef]{40}$/ ) {
786 die "Cannot create tag object $xtag: $!\n";
790 open(C,">$git_dir/refs/tags/$xtag")
791 or die "Cannot create tag $xtag: $!\n";
792 print C "$tagobj\n"
793 or die "Cannot write tag $xtag: $!\n";
794 close(C)
795 or die "Cannot write tag $xtag: $!\n";
797 print "Created tag '$xtag' on '$branch'\n" if $opt_v;
801 my $commitcount = 1;
802 while (<CVS>) {
803 chomp;
804 if ($state == 0 and /^-+$/) {
805 $state = 1;
806 } elsif ($state == 0) {
807 $state = 1;
808 redo;
809 } elsif (($state==0 or $state==1) and s/^PatchSet\s+//) {
810 $patchset = 0+$_;
811 $state=2;
812 } elsif ($state == 2 and s/^Date:\s+//) {
813 $date = pdate($_);
814 unless ($date) {
815 print STDERR "Could not parse date: $_\n";
816 $state=0;
817 next;
819 $state=3;
820 } elsif ($state == 3 and s/^Author:\s+//) {
821 s/\s+$//;
822 if (/^(.*?)\s+<(.*)>/) {
823 ($author_name, $author_email) = ($1, $2);
824 } elsif ($conv_author_name{$_}) {
825 $author_name = $conv_author_name{$_};
826 $author_email = $conv_author_email{$_};
827 } else {
828 $author_name = $author_email = $_;
830 $state = 4;
831 } elsif ($state == 4 and s/^Branch:\s+//) {
832 s/\s+$//;
833 s/[\/]/$opt_s/g;
834 $branch = $_;
835 $state = 5;
836 } elsif ($state == 5 and s/^Ancestor branch:\s+//) {
837 s/\s+$//;
838 $ancestor = $_;
839 $ancestor = $opt_o if $ancestor eq "HEAD";
840 $state = 6;
841 } elsif ($state == 5) {
842 $ancestor = undef;
843 $state = 6;
844 redo;
845 } elsif ($state == 6 and s/^Tag:\s+//) {
846 s/\s+$//;
847 if ($_ eq "(none)") {
848 $tag = undef;
849 } else {
850 $tag = $_;
852 $state = 7;
853 } elsif ($state == 7 and /^Log:/) {
854 $logmsg = "";
855 $state = 8;
856 } elsif ($state == 8 and /^Members:/) {
857 $branch = $opt_o if $branch eq "HEAD";
858 if (defined $branch_date{$branch} and $branch_date{$branch} >= $date) {
859 # skip
860 print "skip patchset $patchset: $date before $branch_date{$branch}\n" if $opt_v;
861 $state = 11;
862 next;
864 if (!$opt_a && $starttime - 300 - (defined $opt_z ? $opt_z : 300) <= $date) {
865 # skip if the commit is too recent
866 # that the cvsps default fuzz is 300s, we give ourselves another
867 # 300s just in case -- this also prevents skipping commits
868 # due to server clock drift
869 print "skip patchset $patchset: $date too recent\n" if $opt_v;
870 $state = 11;
871 next;
873 if (exists $ignorebranch{$branch}) {
874 print STDERR "Skipping $branch\n";
875 $state = 11;
876 next;
878 if ($ancestor) {
879 if ($ancestor eq $branch) {
880 print STDERR "Branch $branch erroneously stems from itself -- changed ancestor to $opt_o\n";
881 $ancestor = $opt_o;
883 if (-f "$git_dir/refs/heads/$branch") {
884 print STDERR "Branch $branch already exists!\n";
885 $state=11;
886 next;
888 unless (open(H,"$git_dir/refs/heads/$ancestor")) {
889 print STDERR "Branch $ancestor does not exist!\n";
890 $ignorebranch{$branch} = 1;
891 $state=11;
892 next;
894 chomp(my $id = <H>);
895 close(H);
896 unless (open(H,"> $git_dir/refs/heads/$branch")) {
897 print STDERR "Could not create branch $branch: $!\n";
898 $ignorebranch{$branch} = 1;
899 $state=11;
900 next;
902 print H "$id\n"
903 or die "Could not write branch $branch: $!";
904 close(H)
905 or die "Could not write branch $branch: $!";
907 $last_branch = $branch if $branch ne $last_branch;
908 $state = 9;
909 } elsif ($state == 8) {
910 $logmsg .= "$_\n";
911 } elsif ($state == 9 and /^\s+(.+?):(INITIAL|\d+(?:\.\d+)+)->(\d+(?:\.\d+)+)\s*$/) {
912 # VERSION:1.96->1.96.2.1
913 my $init = ($2 eq "INITIAL");
914 my $fn = $1;
915 my $rev = $3;
916 $fn =~ s#^/+##;
917 if ($opt_S && $fn =~ m/$opt_S/) {
918 print "SKIPPING $fn v $rev\n";
919 push(@skipped, $fn);
920 next;
922 print "Fetching $fn v $rev\n" if $opt_v;
923 my ($tmpname, $size) = $cvs->file($fn,$rev);
924 if ($size == -1) {
925 push(@old,$fn);
926 print "Drop $fn\n" if $opt_v;
927 } else {
928 print "".($init ? "New" : "Update")." $fn: $size bytes\n" if $opt_v;
929 my $pid = open(my $F, '-|');
930 die $! unless defined $pid;
931 if (!$pid) {
932 exec("git-hash-object", "-w", $tmpname)
933 or die "Cannot create object: $!\n";
935 my $sha = <$F>;
936 chomp $sha;
937 close $F;
938 my $mode = pmode($cvs->{'mode'});
939 push(@new,[$mode, $sha, $fn]); # may be resurrected!
941 unlink($tmpname);
942 } elsif ($state == 9 and /^\s+(.+?):\d+(?:\.\d+)+->(\d+(?:\.\d+)+)\(DEAD\)\s*$/) {
943 my $fn = $1;
944 $fn =~ s#^/+##;
945 push(@old,$fn);
946 print "Delete $fn\n" if $opt_v;
947 } elsif ($state == 9 and /^\s*$/) {
948 $state = 10;
949 } elsif (($state == 9 or $state == 10) and /^-+$/) {
950 $commitcount++;
951 if ($opt_L && $commitcount > $opt_L) {
952 last;
954 commit();
955 if (($commitcount & 1023) == 0) {
956 system("git repack -a -d");
958 $state = 1;
959 } elsif ($state == 11 and /^-+$/) {
960 $state = 1;
961 } elsif (/^-+$/) { # end of unknown-line processing
962 $state = 1;
963 } elsif ($state != 11) { # ignore stuff when skipping
964 print "* UNKNOWN LINE * $_\n";
967 commit() if $branch and $state != 11;
969 unless ($opt_P) {
970 unlink($cvspsfile);
973 # The heuristic of repacking every 1024 commits can leave a
974 # lot of unpacked data. If there is more than 1MB worth of
975 # not-packed objects, repack once more.
976 my $line = `git-count-objects`;
977 if ($line =~ /^(\d+) objects, (\d+) kilobytes$/) {
978 my ($n_objects, $kb) = ($1, $2);
979 1024 < $kb
980 and system("git repack -a -d");
983 foreach my $git_index (values %index) {
984 if ($git_index ne '.git/index') {
985 unlink($git_index);
989 if (defined $orig_git_index) {
990 $ENV{GIT_INDEX_FILE} = $orig_git_index;
991 } else {
992 delete $ENV{GIT_INDEX_FILE};
995 # Now switch back to the branch we were in before all of this happened
996 if ($orig_branch) {
997 print "DONE.\n" if $opt_v;
998 if ($opt_i) {
999 exit 0;
1001 my $tip_at_end = `git-rev-parse --verify HEAD`;
1002 if ($tip_at_start ne $tip_at_end) {
1003 for ($tip_at_start, $tip_at_end) { chomp; }
1004 print "Fetched into the current branch.\n" if $opt_v;
1005 system(qw(git-read-tree -u -m),
1006 $tip_at_start, $tip_at_end);
1007 die "Fast-forward update failed: $?\n" if $?;
1009 else {
1010 system(qw(git-merge cvsimport HEAD), "refs/heads/$opt_o");
1011 die "Could not merge $opt_o into the current branch.\n" if $?;
1013 } else {
1014 $orig_branch = "master";
1015 print "DONE; creating $orig_branch branch\n" if $opt_v;
1016 system("git-update-ref", "refs/heads/master", "refs/heads/$opt_o")
1017 unless -f "$git_dir/refs/heads/master";
1018 system('git-update-ref', 'HEAD', "$orig_branch");
1019 unless ($opt_i) {
1020 system('git checkout');
1021 die "checkout failed: $?\n" if $?;