]> git.ipfire.org Git - thirdparty/git.git/blame - git-cvsimport.perl
Fix lseek(2) calls with args 2 and 3 swapped
[thirdparty/git.git] / git-cvsimport.perl
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 19use File::Spec;
7ccd9009 20use File::Temp qw(tempfile tmpnam);
a57a9493
MU
21use File::Path qw(mkpath);
22use File::Basename qw(basename dirname);
23use Time::Local;
2a3e1a85
MU
24use IO::Socket;
25use IO::Pipe;
e49289df 26use POSIX qw(strftime dup2 ENOENT);
0d821d4d 27use IPC::Open2;
a57a9493
MU
28
29$SIG{'PIPE'}="IGNORE";
30$ENV{'TZ'}="UTC";
31
ded9f400 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, $opt_a);
ffd97f3a 33my (%conv_author_name, %conv_author_email);
a57a9493
MU
34
35sub usage() {
36 print STDERR <<END;
2a3e1a85 37Usage: ${\basename $0} # fetch/update GIT from CVS
ffd97f3a
AE
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]
ded9f400 40 [-s subst] [-a] [-m] [-M regex] [-S regex] [CVS_module]
a57a9493
MU
41END
42 exit(1);
43}
44
ffd97f3a
AE
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>) {
8cd16211 51 # Expected format is this:
ffd97f3a 52 # exon=Andreas Ericsson <ae@op5.se>
8cd16211 53 if (m/^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/) {
ffd97f3a 54 $user = $1;
8cd16211
JH
55 $conv_author_name{$user} = $2;
56 $conv_author_email{$user} = $3;
ffd97f3a 57 }
8cd16211
JH
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?
ffd97f3a
AE
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) {
8cd16211 83 print $f "$_=$conv_author_name{$_} <$conv_author_email{$_}>\n";
ffd97f3a
AE
84 }
85 close ($f);
86}
87
ed35dece
JB
88# convert getopts specs for use by git-repo-config
89sub 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 !~ /:$/);
98
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;
105 }
106 }
107 }
108 if (@ARGV == 0) {
109 chomp(my $module = `git-repo-config --get cvsimport.module`);
110 push(@ARGV, $module);
111 }
112}
113
114my $opts = "haivmkuo:d:p:C:z:s:M:P:A:S:L:";
115read_repo_config($opts);
116getopts($opts) or usage();
a57a9493
MU
117usage if $opt_h;
118
f9714a4a 119@ARGV <= 1 or usage();
a57a9493 120
86d11cf2 121if ($opt_d) {
2a3e1a85 122 $ENV{"CVSROOT"} = $opt_d;
86d11cf2 123} elsif (-f 'CVS/Root') {
f9714a4a
SV
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;
86d11cf2 129} elsif ($ENV{"CVSROOT"}) {
2a3e1a85
MU
130 $opt_d = $ENV{"CVSROOT"};
131} else {
132 die "CVSROOT needs to be set";
133}
134$opt_o ||= "origin";
fbfd60d6 135$opt_s ||= "-";
ded9f400
ML
136$opt_a ||= 0;
137
f9714a4a 138my $git_tree = $opt_C;
2a3e1a85
MU
139$git_tree ||= ".";
140
f9714a4a
SV
141my $cvs_tree;
142if ($#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;
db4b6582 149 close $f;
f9714a4a
SV
150} else {
151 usage();
152}
153
db4b6582
ML
154our @mergerx = ();
155if ($opt_m) {
156 @mergerx = ( qr/\W(?:from|of|merge|merging|merged) (\w+)/i );
157}
158if ($opt_M) {
159 push (@mergerx, qr/$opt_M/);
160}
161
6211988f
ML
162# Remember UTC of our starting time
163# we'll want to avoid importing commits
164# that are too recent
165our $starttime = time();
166
a57a9493
MU
167select(STDERR); $|=1; select(STDOUT);
168
169
170package CVSconn;
171# Basic CVS dialog.
2a3e1a85 172# We're only interested in connecting and downloading, so ...
a57a9493 173
2eb6d82e
SV
174use File::Spec;
175use File::Temp qw(tempfile);
f65ae603
MU
176use POSIX qw(strftime dup2);
177
a57a9493 178sub new {
86d11cf2 179 my ($what,$repo,$subdir) = @_;
a57a9493
MU
180 $what=ref($what) if ref($what);
181
182 my $self = {};
183 $self->{'buffer'} = "";
184 bless($self,$what);
185
186 $repo =~ s#/+$##;
187 $self->{'fullrep'} = $repo;
188 $self->conn();
189
190 $self->{'subdir'} = $subdir;
191 $self->{'lines'} = undef;
192
193 return $self;
194}
195
196sub conn {
197 my $self = shift;
198 my $repo = $self->{'fullrep'};
86d11cf2
JH
199 if ($repo =~ s/^:pserver(?:([^:]*)):(?:(.*?)(?::(.*?))?@)?([^:\/]*)(?::(\d*))?//) {
200 my ($param,$user,$pass,$serv,$port) = ($1,$2,$3,$4,$5);
73bcf533 201
86d11cf2
JH
202 my ($proxyhost,$proxyport);
203 if ($param && ($param =~ m/proxy=([^;]+)/)) {
73bcf533
IA
204 $proxyhost = $1;
205 # Default proxyport, if not specified, is 8080.
206 $proxyport = 8080;
86d11cf2 207 if ($ENV{"CVS_PROXY_PORT"}) {
73bcf533
IA
208 $proxyport = $ENV{"CVS_PROXY_PORT"};
209 }
86d11cf2 210 if ($param =~ m/proxyport=([^;]+)/) {
73bcf533
IA
211 $proxyport = $1;
212 }
213 }
214
a57a9493 215 $user="anonymous" unless defined $user;
2a3e1a85 216 my $rr2 = "-";
86d11cf2 217 unless ($port) {
a57a9493
MU
218 $rr2 = ":pserver:$user\@$serv:$repo";
219 $port=2401;
220 }
221 my $rr = ":pserver:$user\@$serv:$port$repo";
222
86d11cf2 223 unless ($pass) {
a57a9493
MU
224 open(H,$ENV{'HOME'}."/.cvspass") and do {
225 # :pserver:cvs@mea.tmt.tele.fi:/cvsroot/zmailer Ah<Z
86d11cf2 226 while (<H>) {
a57a9493
MU
227 chomp;
228 s/^\/\d+\s+//;
229 my ($w,$p) = split(/\s/,$_,2);
86d11cf2 230 if ($w eq $rr or $w eq $rr2) {
a57a9493
MU
231 $pass = $p;
232 last;
233 }
234 }
235 };
236 }
237 $pass="A" unless $pass;
238
73bcf533 239 my ($s, $rep);
86d11cf2 240 if ($proxyhost) {
73bcf533
IA
241
242 # Use a HTTP Proxy. Only works for HTTP proxies that
243 # don't require user authentication
244 #
245 # See: http://www.ietf.org/rfc/rfc2817.txt
246
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();
252
253 $rep = <$s>;
254
255 # The answer should look like 'HTTP/1.x 2yy ....'
86d11cf2 256 if (!($rep =~ m#^HTTP/1\.. 2[0-9][0-9]#)) {
73bcf533
IA
257 die "Proxy connect: $rep\n";
258 }
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");
265 }
266 } else {
267 $s = IO::Socket::INET->new(PeerHost => $serv, PeerPort => $port);
268 die "Socket to $serv: $!\n" unless defined $s;
269 }
270
a57a9493
MU
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();
274
73bcf533 275 $rep = <$s>;
a57a9493 276
86d11cf2 277 if ($rep ne "I LOVE YOU\n") {
a57a9493
MU
278 $rep="<unknown>" unless $rep;
279 die "AuthReply: $rep\n";
280 }
281 $self->{'socketo'} = $s;
282 $self->{'socketi'} = $s;
34155390 283 } else { # local or ext: Fork off our own cvs server.
a57a9493
MU
284 my $pr = IO::Pipe->new();
285 my $pw = IO::Pipe->new();
286 my $pid = fork();
287 die "Fork: $!\n" unless defined $pid;
8d0ea311
SV
288 my $cvs = 'cvs';
289 $cvs = $ENV{CVS_SERVER} if exists $ENV{CVS_SERVER};
34155390
SV
290 my $rsh = 'rsh';
291 $rsh = $ENV{CVS_RSH} if exists $ENV{CVS_RSH};
292
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);
300 }
301 if (!$local) {
302 if ($user) {
303 unshift @cvs, $rsh, '-l', $user, $host;
304 } else {
305 unshift @cvs, $rsh, $host;
306 }
307 }
308
86d11cf2 309 unless ($pid) {
a57a9493
MU
310 $pr->writer();
311 $pw->reader();
a57a9493
MU
312 dup2($pw->fileno(),0);
313 dup2($pr->fileno(),1);
314 $pr->close();
315 $pw->close();
34155390 316 exec(@cvs);
a57a9493
MU
317 }
318 $pw->writer();
319 $pr->reader();
320 $self->{'socketo'} = $pw;
321 $self->{'socketi'} = $pr;
322 }
323 $self->{'socketo'}->write("Root $repo\n");
324
325 # Trial and error says that this probably is the minimum set
b0921331 326 $self->{'socketo'}->write("Valid-responses ok error Valid-requests Mode M Mbinary E Checked-in Created Updated Merged Removed\n");
a57a9493
MU
327
328 $self->{'socketo'}->write("valid-requests\n");
329 $self->{'socketo'}->flush();
330
331 chomp(my $rep=$self->readline());
86d11cf2 332 if ($rep !~ s/^Valid-requests\s*//) {
a57a9493
MU
333 $rep="<unknown>" unless $rep;
334 die "Expected Valid-requests from server, but got: $rep\n";
335 }
336 chomp(my $res=$self->readline());
337 die "validReply: $res\n" if $res ne "ok";
338
339 $self->{'socketo'}->write("UseUnchanged\n") if $rep =~ /\bUseUnchanged\b/;
340 $self->{'repo'} = $repo;
341}
342
343sub readline {
86d11cf2 344 my ($self) = @_;
a57a9493
MU
345 return $self->{'socketi'}->getline();
346}
347
348sub _file {
349 # Request a file with a given revision.
350 # Trial and error says this is a good way to do it. :-/
86d11cf2 351 my ($self,$fn,$rev) = @_;
a57a9493
MU
352 $self->{'socketo'}->write("Argument -N\n") or return undef;
353 $self->{'socketo'}->write("Argument -P\n") or return undef;
abe05822
ML
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;
357 }
a57a9493
MU
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;
4f7c0caa 364 # $self->{'socketo'}->write("Sticky T1.0\n") or return undef;
a57a9493
MU
365 $self->{'socketo'}->write("co\n") or return undef;
366 $self->{'socketo'}->flush() or return undef;
367 $self->{'lines'} = 0;
368 return 1;
369}
370sub _line {
371 # Read a line from the server.
372 # ... except that 'line' may be an entire file. ;-)
86d11cf2 373 my ($self, $fh) = @_;
a57a9493
MU
374 die "Not in lines" unless defined $self->{'lines'};
375
376 my $line;
2eb6d82e 377 my $res=0;
86d11cf2 378 while (defined($line = $self->readline())) {
a57a9493
MU
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
384 # 0
385 # ok
386
86d11cf2 387 if ($line =~ s/^(?:Created|Updated) //) {
a57a9493
MU
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="";
55cad842 397 $res = $self->_fetchfile($fh, $cnt);
86d11cf2 398 } elsif ($line =~ s/^ //) {
2eb6d82e
SV
399 print $fh $line;
400 $res += length($line);
86d11cf2 401 } elsif ($line =~ /^M\b/) {
a57a9493 402 # output, do nothing
86d11cf2 403 } elsif ($line =~ /^Mbinary\b/) {
a57a9493
MU
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="";
55cad842 409 $res += $self->_fetchfile($fh, $cnt);
a57a9493
MU
410 } else {
411 chomp $line;
86d11cf2 412 if ($line eq "ok") {
a57a9493
MU
413 # print STDERR "S: ok (".length($res).")\n";
414 return $res;
86d11cf2 415 } elsif ($line =~ s/^E //) {
a57a9493 416 # print STDERR "S: $line\n";
86d11cf2 417 } elsif ($line =~ /^(Remove-entry|Removed) /i) {
8b8840e0
MU
418 $line = $self->readline(); # filename
419 $line = $self->readline(); # OK
420 chomp $line;
421 die "Unknown: $line" if $line ne "ok";
422 return -1;
a57a9493
MU
423 } else {
424 die "Unknown: $line\n";
425 }
426 }
427 }
39ba7d54 428 return undef;
a57a9493
MU
429}
430sub file {
86d11cf2 431 my ($self,$fn,$rev) = @_;
a57a9493
MU
432 my $res;
433
2eb6d82e
SV
434 my ($fh, $name) = tempfile('gitcvs.XXXXXX',
435 DIR => File::Spec->tmpdir(), UNLINK => 1);
436
437 $self->_file($fn,$rev) and $res = $self->_line($fh);
438
439 if (!defined $res) {
39ba7d54
MM
440 print STDERR "Server has gone away while fetching $fn $rev, retrying...\n";
441 truncate $fh, 0;
2eb6d82e 442 $self->conn();
39ba7d54 443 $self->_file($fn,$rev) or die "No file command send";
2eb6d82e 444 $res = $self->_line($fh);
39ba7d54 445 die "Retry failed" unless defined $res;
a57a9493 446 }
c619ad51 447 close ($fh);
a57a9493 448
2eb6d82e 449 return ($name, $res);
a57a9493 450}
55cad842
ML
451sub _fetchfile {
452 my ($self, $fh, $cnt) = @_;
61efa5e3 453 my $res = 0;
55cad842 454 my $bufsize = 1024 * 1024;
86d11cf2 455 while ($cnt) {
55cad842
ML
456 if ($bufsize > $cnt) {
457 $bufsize = $cnt;
458 }
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;
465 }
466 return $res;
467}
a57a9493
MU
468
469
470package main;
471
2a3e1a85 472my $cvs = CVSconn->new($opt_d, $cvs_tree);
a57a9493
MU
473
474
475sub pdate($) {
86d11cf2 476 my ($d) = @_;
a57a9493
MU
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);
9718a00b
TM
481}
482
a57a9493 483sub pmode($) {
86d11cf2 484 my ($mode) = @_;
a57a9493
MU
485 my $m = 0;
486 my $mm = 0;
487 my $um = 0;
488 for my $x(split(//,$mode)) {
86d11cf2 489 if ($x eq ",") {
a57a9493
MU
490 $m |= $mm&$um;
491 $mm = 0;
492 $um = 0;
86d11cf2
JH
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
a57a9493
MU
500 } else { die "Unknown mode: $mode\n";
501 }
502 }
503 $m |= $mm&$um;
504 return $m;
505}
d4f8b390 506
a57a9493
MU
507sub getwd() {
508 my $pwd = `pwd`;
509 chomp $pwd;
510 return $pwd;
d4f8b390
LT
511}
512
e73aefe4
JK
513sub is_sha1 {
514 my $s = shift;
515 return $s =~ /^[a-f0-9]{40}$/;
516}
db4b6582 517
e73aefe4 518sub get_headref ($$) {
db4b6582
ML
519 my $name = shift;
520 my $git_dir = shift;
db4b6582 521
e73aefe4 522 my $f = "$git_dir/refs/heads/$name";
86d11cf2 523 if (open(my $fh, $f)) {
e73aefe4
JK
524 chomp(my $r = <$fh>);
525 is_sha1($r) or die "Cannot get head id for $name ($r): $!";
526 return $r;
db4b6582 527 }
e73aefe4
JK
528 die "unable to open $f: $!" unless $! == POSIX::ENOENT;
529 return undef;
db4b6582
ML
530}
531
a57a9493
MU
532-d $git_tree
533 or mkdir($git_tree,0777)
534 or die "Could not create $git_tree: $!";
535chdir($git_tree);
d4f8b390 536
a57a9493 537my $last_branch = "";
46541669 538my $orig_branch = "";
a57a9493 539my %branch_date;
8a5f2eac 540my $tip_at_start = undef;
a57a9493
MU
541
542my $git_dir = $ENV{"GIT_DIR"} || ".git";
543$git_dir = getwd()."/".$git_dir unless $git_dir =~ m#^/#;
544$ENV{"GIT_DIR"} = $git_dir;
79ee456c
SV
545my $orig_git_index;
546$orig_git_index = $ENV{GIT_INDEX_FILE} if exists $ENV{GIT_INDEX_FILE};
8f732649
ML
547
548my %index; # holds filenames of one index per branch
061303f0 549
86d11cf2 550unless (-d $git_dir) {
5c94f87e 551 system("git-init");
a57a9493
MU
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 $?;
555
556 $last_branch = $opt_o;
46541669 557 $orig_branch = "";
a57a9493 558} else {
866d1310 559 -f "$git_dir/refs/heads/$opt_o"
4c24e089
MU
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";
563
8366a10a
PR
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);
86d11cf2 569 unless ($last_branch) {
46541669
MU
570 warn "Cannot read the last branch name: $! -- assuming 'master'\n";
571 $last_branch = "master";
572 }
573 $orig_branch = $last_branch;
8a5f2eac 574 $tip_at_start = `git-rev-parse --verify HEAD`;
a57a9493
MU
575
576 # Get the last import timestamps
1f24c587
AW
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";
86d11cf2 580 while (defined(my $entry = <H>)) {
1f24c587
AW
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;
a57a9493 586 }
1f24c587 587 close(H);
a57a9493
MU
588}
589
590-d $git_dir
591 or die "Could not create git subdir ($git_dir).\n";
592
ffd97f3a
AE
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");
596if ($opt_A) {
597 read_author_info($opt_A);
598 write_author_info("$git_dir/cvs-authors");
599}
600
2f57c697
ML
601
602#
603# run cvsps into a file unless we are getting
604# it passed as a file via $opt_P
605#
4083c2fc 606my $cvspsfile;
2f57c697
ML
607unless ($opt_P) {
608 print "Running cvsps...\n" if $opt_v;
609 my $pid = open(CVSPS,"-|");
4083c2fc 610 my $cvspsfh;
2f57c697 611 die "Cannot fork: $!\n" unless defined $pid;
86d11cf2 612 unless ($pid) {
2f57c697
ML
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';
619 }
620 exec("cvsps","--norc",@opt,"-u","-A",'--root',$opt_d,$cvs_tree);
621 die "Could not start cvsps: $!\n";
df73e9c6 622 }
4083c2fc
ML
623 ($cvspsfh, $cvspsfile) = tempfile('gitXXXXXX', SUFFIX => '.cvsps',
624 DIR => File::Spec->tmpdir());
2f57c697
ML
625 while (<CVSPS>) {
626 print $cvspsfh $_;
211dcac6 627 }
2f57c697
ML
628 close CVSPS;
629 close $cvspsfh;
4083c2fc
ML
630} else {
631 $cvspsfile = $opt_P;
a57a9493
MU
632}
633
4083c2fc 634open(CVS, "<$cvspsfile") or die $!;
2f57c697 635
a57a9493
MU
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
649#
650#---------------------
651
652my $state = 0;
653
e73aefe4
JK
654sub update_index (\@\@) {
655 my $old = shift;
656 my $new = shift;
6a1871e1
JK
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" }
e73aefe4 661 @$old),
6a1871e1 662 (map { '100' . sprintf('%o', $_->[0]) . " $_->[1]\t$_->[2]\0" }
e73aefe4 663 @$new)
6a1871e1
JK
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: $?";
e73aefe4 668}
a57a9493 669
e73aefe4
JK
670sub 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)
a57a9493
MU
677 or die "Error running git-write-tree: $?\n";
678 print "Tree ID $tree\n" if $opt_v;
e73aefe4
JK
679 return $tree;
680}
a57a9493 681
86d11cf2
JH
682my ($patchset,$date,$author_name,$author_email,$branch,$ancestor,$tag,$logmsg);
683my (@old,@new,@skipped,%ignorebranch);
71b08148
ML
684
685# commits that cvsps cannot place anywhere...
686$ignorebranch{'#CVSPS_NO_BRANCH'} = 1;
687
e73aefe4 688sub commit {
c5f448b0
ML
689 if ($branch eq $opt_o && !$index{branch} && !get_headref($branch, $git_dir)) {
690 # looks like an initial commit
5c94f87e 691 # use the index primed by git-init
c5f448b0
ML
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);
704 }
705 die "read-tree failed: $?\n" if $?;
706 }
707 }
708 $ENV{GIT_INDEX_FILE} = $index{$branch};
709
e73aefe4
JK
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;
715
716 my @commit_args;
717 push @commit_args, ("-p", $parent) if $parent;
718
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;
86d11cf2 724 if (my $sha1 = get_headref($mparent, $git_dir)) {
e73aefe4
JK
725 push @commit_args, '-p', $mparent;
726 print "Merge parent branch: $mparent\n" if $opt_v;
db4b6582 727 }
a57a9493 728 }
e73aefe4
JK
729
730 my $commit_date = strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date));
62bf0d96
JK
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;
e73aefe4 737 my $pid = open2(my $commit_read, my $commit_write,
e73aefe4 738 'git-commit-tree', $tree, @commit_args);
e371046b
MU
739
740 # compatibility with git2cvs
741 substr($logmsg,32767) = "" if length($logmsg) > 32767;
742 $logmsg =~ s/[\s\n]+\z//;
743
5179c8a5
ML
744 if (@skipped) {
745 $logmsg .= "\n\n\nSKIPPED:\n\t";
746 $logmsg .= join("\n\t", @skipped) . "\n";
f396f01f 747 @skipped = ();
5179c8a5
ML
748 }
749
e73aefe4 750 print($commit_write "$logmsg\n") && close($commit_write)
a57a9493 751 or die "Error writing to git-commit-tree: $!\n";
2a3e1a85 752
e73aefe4
JK
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";
a57a9493 756 print "Commit ID $cid\n" if $opt_v;
e73aefe4 757 close($commit_read);
2a3e1a85
MU
758
759 waitpid($pid,0);
760 die "Error running git-commit-tree: $?\n" if $?;
a57a9493 761
42277bc8 762 system("git-update-ref refs/heads/$branch $cid") == 0
a57a9493
MU
763 or die "Cannot write branch $branch for update: $!\n";
764
86d11cf2
JH
765 if ($tag) {
766 my ($in, $out) = ('','');
767 my ($xtag) = $tag;
0d821d4d
PA
768 $xtag =~ s/\s+\*\*.*$//; # Remove stuff like ** INVALID ** and ** FUNKY **
769 $xtag =~ tr/_/\./ if ( $opt_u );
34c99da2 770 $xtag =~ s/[\/]/$opt_s/g;
0d821d4d
PA
771
772 my $pid = open2($in, $out, 'git-mktag');
773 print $out "object $cid\n".
774 "type commit\n".
775 "tag $xtag\n".
94c23343 776 "tagger $author_name <$author_email>\n"
0d821d4d
PA
777 or die "Cannot create tag object $xtag: $!\n";
778 close($out)
779 or die "Cannot create tag object $xtag: $!\n";
780
781 my $tagobj = <$in>;
782 chomp $tagobj;
783
784 if ( !close($in) or waitpid($pid, 0) != $pid or
785 $? != 0 or $tagobj !~ /^[0123456789abcdef]{40}$/ ) {
786 die "Cannot create tag object $xtag: $!\n";
787 }
788
789
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";
a57a9493 794 close(C)
0d821d4d
PA
795 or die "Cannot write tag $xtag: $!\n";
796
797 print "Created tag '$xtag' on '$branch'\n" if $opt_v;
a57a9493 798 }
a57a9493
MU
799};
800
06918348 801my $commitcount = 1;
86d11cf2 802while (<CVS>) {
a57a9493 803 chomp;
86d11cf2 804 if ($state == 0 and /^-+$/) {
a57a9493 805 $state = 1;
86d11cf2 806 } elsif ($state == 0) {
a57a9493
MU
807 $state = 1;
808 redo;
86d11cf2 809 } elsif (($state==0 or $state==1) and s/^PatchSet\s+//) {
a57a9493
MU
810 $patchset = 0+$_;
811 $state=2;
86d11cf2 812 } elsif ($state == 2 and s/^Date:\s+//) {
a57a9493 813 $date = pdate($_);
86d11cf2 814 unless ($date) {
a57a9493
MU
815 print STDERR "Could not parse date: $_\n";
816 $state=0;
817 next;
818 }
819 $state=3;
86d11cf2 820 } elsif ($state == 3 and s/^Author:\s+//) {
a57a9493 821 s/\s+$//;
94c23343
JH
822 if (/^(.*?)\s+<(.*)>/) {
823 ($author_name, $author_email) = ($1, $2);
ffd97f3a
AE
824 } elsif ($conv_author_name{$_}) {
825 $author_name = $conv_author_name{$_};
826 $author_email = $conv_author_email{$_};
94c23343
JH
827 } else {
828 $author_name = $author_email = $_;
829 }
a57a9493 830 $state = 4;
86d11cf2 831 } elsif ($state == 4 and s/^Branch:\s+//) {
a57a9493 832 s/\s+$//;
fbfd60d6 833 s/[\/]/$opt_s/g;
a57a9493
MU
834 $branch = $_;
835 $state = 5;
86d11cf2 836 } elsif ($state == 5 and s/^Ancestor branch:\s+//) {
a57a9493
MU
837 s/\s+$//;
838 $ancestor = $_;
0fa2824f 839 $ancestor = $opt_o if $ancestor eq "HEAD";
a57a9493 840 $state = 6;
86d11cf2 841 } elsif ($state == 5) {
a57a9493
MU
842 $ancestor = undef;
843 $state = 6;
844 redo;
86d11cf2 845 } elsif ($state == 6 and s/^Tag:\s+//) {
a57a9493 846 s/\s+$//;
86d11cf2 847 if ($_ eq "(none)") {
a57a9493
MU
848 $tag = undef;
849 } else {
850 $tag = $_;
851 }
852 $state = 7;
86d11cf2 853 } elsif ($state == 7 and /^Log:/) {
a57a9493
MU
854 $logmsg = "";
855 $state = 8;
86d11cf2 856 } elsif ($state == 8 and /^Members:/) {
a57a9493 857 $branch = $opt_o if $branch eq "HEAD";
86d11cf2 858 if (defined $branch_date{$branch} and $branch_date{$branch} >= $date) {
a57a9493 859 # skip
9da07f34 860 print "skip patchset $patchset: $date before $branch_date{$branch}\n" if $opt_v;
a57a9493
MU
861 $state = 11;
862 next;
863 }
ded9f400 864 if (!$opt_a && $starttime - 300 - (defined $opt_z ? $opt_z : 300) <= $date) {
6211988f
ML
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;
872 }
71b08148
ML
873 if (exists $ignorebranch{$branch}) {
874 print STDERR "Skipping $branch\n";
875 $state = 11;
876 next;
877 }
86d11cf2
JH
878 if ($ancestor) {
879 if ($ancestor eq $branch) {
71b08148
ML
880 print STDERR "Branch $branch erroneously stems from itself -- changed ancestor to $opt_o\n";
881 $ancestor = $opt_o;
882 }
86d11cf2 883 if (-f "$git_dir/refs/heads/$branch") {
a57a9493
MU
884 print STDERR "Branch $branch already exists!\n";
885 $state=11;
886 next;
887 }
86d11cf2 888 unless (open(H,"$git_dir/refs/heads/$ancestor")) {
a57a9493 889 print STDERR "Branch $ancestor does not exist!\n";
71b08148 890 $ignorebranch{$branch} = 1;
a57a9493
MU
891 $state=11;
892 next;
893 }
894 chomp(my $id = <H>);
895 close(H);
86d11cf2 896 unless (open(H,"> $git_dir/refs/heads/$branch")) {
a57a9493 897 print STDERR "Could not create branch $branch: $!\n";
71b08148 898 $ignorebranch{$branch} = 1;
a57a9493
MU
899 $state=11;
900 next;
901 }
902 print H "$id\n"
903 or die "Could not write branch $branch: $!";
904 close(H)
905 or die "Could not write branch $branch: $!";
906 }
46e63efc 907 $last_branch = $branch if $branch ne $last_branch;
a57a9493 908 $state = 9;
86d11cf2 909 } elsif ($state == 8) {
a57a9493 910 $logmsg .= "$_\n";
86d11cf2 911 } elsif ($state == 9 and /^\s+(.+?):(INITIAL|\d+(?:\.\d+)+)->(\d+(?:\.\d+)+)\s*$/) {
a57a9493 912# VERSION:1.96->1.96.2.1
2a3e1a85 913 my $init = ($2 eq "INITIAL");
a57a9493 914 my $fn = $1;
f65ae603
MU
915 my $rev = $3;
916 $fn =~ s#^/+##;
5179c8a5
ML
917 if ($opt_S && $fn =~ m/$opt_S/) {
918 print "SKIPPING $fn v $rev\n";
919 push(@skipped, $fn);
920 next;
921 }
922 print "Fetching $fn v $rev\n" if $opt_v;
2eb6d82e 923 my ($tmpname, $size) = $cvs->file($fn,$rev);
86d11cf2 924 if ($size == -1) {
8b8840e0
MU
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;
dd27478f
JH
929 my $pid = open(my $F, '-|');
930 die $! unless defined $pid;
931 if (!$pid) {
932 exec("git-hash-object", "-w", $tmpname)
8b8840e0 933 or die "Cannot create object: $!\n";
dd27478f 934 }
8b8840e0
MU
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!
940 }
2eb6d82e 941 unlink($tmpname);
86d11cf2 942 } elsif ($state == 9 and /^\s+(.+?):\d+(?:\.\d+)+->(\d+(?:\.\d+)+)\(DEAD\)\s*$/) {
f65ae603
MU
943 my $fn = $1;
944 $fn =~ s#^/+##;
945 push(@old,$fn);
8b8840e0 946 print "Delete $fn\n" if $opt_v;
86d11cf2 947 } elsif ($state == 9 and /^\s*$/) {
a57a9493 948 $state = 10;
86d11cf2 949 } elsif (($state == 9 or $state == 10) and /^-+$/) {
4adcea99
LT
950 $commitcount++;
951 if ($opt_L && $commitcount > $opt_L) {
06918348
ML
952 last;
953 }
c4b16f8d 954 commit();
4adcea99
LT
955 if (($commitcount & 1023) == 0) {
956 system("git repack -a -d");
957 }
a57a9493 958 $state = 1;
86d11cf2 959 } elsif ($state == 11 and /^-+$/) {
a57a9493 960 $state = 1;
86d11cf2 961 } elsif (/^-+$/) { # end of unknown-line processing
a57a9493 962 $state = 1;
86d11cf2 963 } elsif ($state != 11) { # ignore stuff when skipping
a57a9493
MU
964 print "* UNKNOWN LINE * $_\n";
965 }
966}
c4b16f8d 967commit() if $branch and $state != 11;
d4f8b390 968
4083c2fc
ML
969unless ($opt_P) {
970 unlink($cvspsfile);
971}
972
efe4abd1
JM
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.
976my $line = `git-count-objects`;
977if ($line =~ /^(\d+) objects, (\d+) kilobytes$/) {
978 my ($n_objects, $kb) = ($1, $2);
979 1024 < $kb
980 and system("git repack -a -d");
981}
982
8f732649 983foreach my $git_index (values %index) {
c5f448b0
ML
984 if ($git_index ne '.git/index') {
985 unlink($git_index);
986 }
8f732649 987}
79ee456c 988
210569f9
SV
989if (defined $orig_git_index) {
990 $ENV{GIT_INDEX_FILE} = $orig_git_index;
991} else {
992 delete $ENV{GIT_INDEX_FILE};
993}
994
46541669 995# Now switch back to the branch we were in before all of this happened
86d11cf2 996if ($orig_branch) {
8a5f2eac
JH
997 print "DONE.\n" if $opt_v;
998 if ($opt_i) {
999 exit 0;
1000 }
1001 my $tip_at_end = `git-rev-parse --verify HEAD`;
1002 if ($tip_at_start ne $tip_at_end) {
cb9594e2 1003 for ($tip_at_start, $tip_at_end) { chomp; }
8a5f2eac
JH
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 $?;
1008 }
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 $?;
1012 }
46541669
MU
1013} else {
1014 $orig_branch = "master";
1015 print "DONE; creating $orig_branch branch\n" if $opt_v;
a541211e 1016 system("git-update-ref", "refs/heads/master", "refs/heads/$opt_o")
46541669 1017 unless -f "$git_dir/refs/heads/master";
8366a10a 1018 system('git-update-ref', 'HEAD', "$orig_branch");
c1c774e7
SV
1019 unless ($opt_i) {
1020 system('git checkout');
1021 die "checkout failed: $?\n" if $?;
1022 }
46541669 1023}