]>
Commit | Line | Data |
---|---|---|
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 | ||
16 | use strict; | |
17 | use warnings; | |
18 | use Getopt::Std; | |
79ee456c | 19 | use File::Spec; |
7ccd9009 | 20 | use File::Temp qw(tempfile tmpnam); |
a57a9493 MU |
21 | use File::Path qw(mkpath); |
22 | use File::Basename qw(basename dirname); | |
23 | use Time::Local; | |
2a3e1a85 MU |
24 | use IO::Socket; |
25 | use IO::Pipe; | |
e49289df | 26 | use POSIX qw(strftime dup2 ENOENT); |
0d821d4d | 27 | use IPC::Open2; |
a57a9493 MU |
28 | |
29 | $SIG{'PIPE'}="IGNORE"; | |
30 | $ENV{'TZ'}="UTC"; | |
31 | ||
06918348 | 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); |
ffd97f3a | 33 | my (%conv_author_name, %conv_author_email); |
a57a9493 MU |
34 | |
35 | sub usage() { | |
36 | print STDERR <<END; | |
2a3e1a85 | 37 | Usage: ${\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] | |
5179c8a5 | 40 | [-s subst] [-m] [-M regex] [-S regex] [CVS_module] |
a57a9493 MU |
41 | END |
42 | exit(1); | |
43 | } | |
44 | ||
ffd97f3a AE |
45 | sub 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 | ||
77 | sub 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 | ||
06918348 | 88 | getopts("hivmkuo:d:p:C:z:s:M:P:A:S:L:") or usage(); |
a57a9493 MU |
89 | usage if $opt_h; |
90 | ||
f9714a4a | 91 | @ARGV <= 1 or usage(); |
a57a9493 | 92 | |
2a3e1a85 MU |
93 | if($opt_d) { |
94 | $ENV{"CVSROOT"} = $opt_d; | |
f9714a4a SV |
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; | |
2a3e1a85 MU |
101 | } elsif($ENV{"CVSROOT"}) { |
102 | $opt_d = $ENV{"CVSROOT"}; | |
103 | } else { | |
104 | die "CVSROOT needs to be set"; | |
105 | } | |
106 | $opt_o ||= "origin"; | |
fbfd60d6 | 107 | $opt_s ||= "-"; |
f9714a4a | 108 | my $git_tree = $opt_C; |
2a3e1a85 MU |
109 | $git_tree ||= "."; |
110 | ||
f9714a4a SV |
111 | my $cvs_tree; |
112 | if ($#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; | |
db4b6582 | 119 | close $f; |
f9714a4a SV |
120 | } else { |
121 | usage(); | |
122 | } | |
123 | ||
db4b6582 ML |
124 | our @mergerx = (); |
125 | if ($opt_m) { | |
126 | @mergerx = ( qr/\W(?:from|of|merge|merging|merged) (\w+)/i ); | |
127 | } | |
128 | if ($opt_M) { | |
129 | push (@mergerx, qr/$opt_M/); | |
130 | } | |
131 | ||
a57a9493 MU |
132 | select(STDERR); $|=1; select(STDOUT); |
133 | ||
134 | ||
135 | package CVSconn; | |
136 | # Basic CVS dialog. | |
2a3e1a85 | 137 | # We're only interested in connecting and downloading, so ... |
a57a9493 | 138 | |
2eb6d82e SV |
139 | use File::Spec; |
140 | use File::Temp qw(tempfile); | |
f65ae603 MU |
141 | use POSIX qw(strftime dup2); |
142 | ||
a57a9493 MU |
143 | sub 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 | ||
161 | sub 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; | |
2a3e1a85 | 167 | my $rr2 = "-"; |
a57a9493 MU |
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 | ||
a57a9493 MU |
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; | |
34155390 | 204 | } else { # local or ext: Fork off our own cvs server. |
a57a9493 MU |
205 | my $pr = IO::Pipe->new(); |
206 | my $pw = IO::Pipe->new(); | |
207 | my $pid = fork(); | |
208 | die "Fork: $!\n" unless defined $pid; | |
8d0ea311 SV |
209 | my $cvs = 'cvs'; |
210 | $cvs = $ENV{CVS_SERVER} if exists $ENV{CVS_SERVER}; | |
34155390 SV |
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 | ||
a57a9493 MU |
230 | unless($pid) { |
231 | $pr->writer(); | |
232 | $pw->reader(); | |
a57a9493 MU |
233 | dup2($pw->fileno(),0); |
234 | dup2($pr->fileno(),1); | |
235 | $pr->close(); | |
236 | $pw->close(); | |
34155390 | 237 | exec(@cvs); |
a57a9493 MU |
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 | |
b0921331 | 247 | $self->{'socketo'}->write("Valid-responses ok error Valid-requests Mode M Mbinary E Checked-in Created Updated Merged Removed\n"); |
a57a9493 MU |
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 | ||
264 | sub readline { | |
265 | my($self) = @_; | |
266 | return $self->{'socketi'}->getline(); | |
267 | } | |
268 | ||
269 | sub _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; | |
abe05822 ML |
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 | } | |
a57a9493 MU |
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; | |
4f7c0caa | 285 | # $self->{'socketo'}->write("Sticky T1.0\n") or return undef; |
a57a9493 MU |
286 | $self->{'socketo'}->write("co\n") or return undef; |
287 | $self->{'socketo'}->flush() or return undef; | |
288 | $self->{'lines'} = 0; | |
289 | return 1; | |
290 | } | |
291 | sub _line { | |
292 | # Read a line from the server. | |
293 | # ... except that 'line' may be an entire file. ;-) | |
2eb6d82e | 294 | my($self, $fh) = @_; |
a57a9493 MU |
295 | die "Not in lines" unless defined $self->{'lines'}; |
296 | ||
297 | my $line; | |
2eb6d82e | 298 | my $res=0; |
a57a9493 MU |
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=""; | |
55cad842 | 318 | $res = $self->_fetchfile($fh, $cnt); |
a57a9493 | 319 | } elsif($line =~ s/^ //) { |
2eb6d82e SV |
320 | print $fh $line; |
321 | $res += length($line); | |
a57a9493 MU |
322 | } elsif($line =~ /^M\b/) { |
323 | # output, do nothing | |
324 | } elsif($line =~ /^Mbinary\b/) { | |
325 | my $cnt; | |
326 | die "EOF from server after 'Mbinary'" unless defined ($cnt = $self->readline()); | |
327 | chomp $cnt; | |
328 | die "Duh: Mbinary $cnt" if $cnt !~ /^\d+$/ or $cnt<1; | |
329 | $line=""; | |
55cad842 | 330 | $res += $self->_fetchfile($fh, $cnt); |
a57a9493 MU |
331 | } else { |
332 | chomp $line; | |
333 | if($line eq "ok") { | |
334 | # print STDERR "S: ok (".length($res).")\n"; | |
335 | return $res; | |
336 | } elsif($line =~ s/^E //) { | |
337 | # print STDERR "S: $line\n"; | |
be0c7e06 | 338 | } elsif($line =~ /^(Remove-entry|Removed) /i) { |
8b8840e0 MU |
339 | $line = $self->readline(); # filename |
340 | $line = $self->readline(); # OK | |
341 | chomp $line; | |
342 | die "Unknown: $line" if $line ne "ok"; | |
343 | return -1; | |
a57a9493 MU |
344 | } else { |
345 | die "Unknown: $line\n"; | |
346 | } | |
347 | } | |
348 | } | |
39ba7d54 | 349 | return undef; |
a57a9493 MU |
350 | } |
351 | sub file { | |
352 | my($self,$fn,$rev) = @_; | |
353 | my $res; | |
354 | ||
2eb6d82e SV |
355 | my ($fh, $name) = tempfile('gitcvs.XXXXXX', |
356 | DIR => File::Spec->tmpdir(), UNLINK => 1); | |
357 | ||
358 | $self->_file($fn,$rev) and $res = $self->_line($fh); | |
359 | ||
360 | if (!defined $res) { | |
39ba7d54 MM |
361 | print STDERR "Server has gone away while fetching $fn $rev, retrying...\n"; |
362 | truncate $fh, 0; | |
2eb6d82e | 363 | $self->conn(); |
39ba7d54 | 364 | $self->_file($fn,$rev) or die "No file command send"; |
2eb6d82e | 365 | $res = $self->_line($fh); |
39ba7d54 | 366 | die "Retry failed" unless defined $res; |
a57a9493 | 367 | } |
c619ad51 | 368 | close ($fh); |
a57a9493 | 369 | |
2eb6d82e | 370 | return ($name, $res); |
a57a9493 | 371 | } |
55cad842 ML |
372 | sub _fetchfile { |
373 | my ($self, $fh, $cnt) = @_; | |
61efa5e3 | 374 | my $res = 0; |
55cad842 ML |
375 | my $bufsize = 1024 * 1024; |
376 | while($cnt) { | |
377 | if ($bufsize > $cnt) { | |
378 | $bufsize = $cnt; | |
379 | } | |
380 | my $buf; | |
381 | my $num = $self->{'socketi'}->read($buf,$bufsize); | |
382 | die "Server: Filesize $cnt: $num: $!\n" if not defined $num or $num<=0; | |
383 | print $fh $buf; | |
384 | $res += $num; | |
385 | $cnt -= $num; | |
386 | } | |
387 | return $res; | |
388 | } | |
a57a9493 MU |
389 | |
390 | ||
391 | package main; | |
392 | ||
2a3e1a85 | 393 | my $cvs = CVSconn->new($opt_d, $cvs_tree); |
a57a9493 MU |
394 | |
395 | ||
396 | sub pdate($) { | |
397 | my($d) = @_; | |
398 | m#(\d{2,4})/(\d\d)/(\d\d)\s(\d\d):(\d\d)(?::(\d\d))?# | |
399 | or die "Unparseable date: $d\n"; | |
400 | my $y=$1; $y-=1900 if $y>1900; | |
401 | return timegm($6||0,$5,$4,$3,$2-1,$y); | |
9718a00b TM |
402 | } |
403 | ||
a57a9493 MU |
404 | sub pmode($) { |
405 | my($mode) = @_; | |
406 | my $m = 0; | |
407 | my $mm = 0; | |
408 | my $um = 0; | |
409 | for my $x(split(//,$mode)) { | |
410 | if($x eq ",") { | |
411 | $m |= $mm&$um; | |
412 | $mm = 0; | |
413 | $um = 0; | |
414 | } elsif($x eq "u") { $um |= 0700; | |
415 | } elsif($x eq "g") { $um |= 0070; | |
416 | } elsif($x eq "o") { $um |= 0007; | |
417 | } elsif($x eq "r") { $mm |= 0444; | |
418 | } elsif($x eq "w") { $mm |= 0222; | |
419 | } elsif($x eq "x") { $mm |= 0111; | |
420 | } elsif($x eq "=") { # do nothing | |
421 | } else { die "Unknown mode: $mode\n"; | |
422 | } | |
423 | } | |
424 | $m |= $mm&$um; | |
425 | return $m; | |
426 | } | |
d4f8b390 | 427 | |
a57a9493 MU |
428 | sub getwd() { |
429 | my $pwd = `pwd`; | |
430 | chomp $pwd; | |
431 | return $pwd; | |
d4f8b390 LT |
432 | } |
433 | ||
e73aefe4 JK |
434 | sub is_sha1 { |
435 | my $s = shift; | |
436 | return $s =~ /^[a-f0-9]{40}$/; | |
437 | } | |
db4b6582 | 438 | |
e73aefe4 | 439 | sub get_headref ($$) { |
db4b6582 ML |
440 | my $name = shift; |
441 | my $git_dir = shift; | |
db4b6582 | 442 | |
e73aefe4 JK |
443 | my $f = "$git_dir/refs/heads/$name"; |
444 | if(open(my $fh, $f)) { | |
445 | chomp(my $r = <$fh>); | |
446 | is_sha1($r) or die "Cannot get head id for $name ($r): $!"; | |
447 | return $r; | |
db4b6582 | 448 | } |
e73aefe4 JK |
449 | die "unable to open $f: $!" unless $! == POSIX::ENOENT; |
450 | return undef; | |
db4b6582 ML |
451 | } |
452 | ||
a57a9493 MU |
453 | -d $git_tree |
454 | or mkdir($git_tree,0777) | |
455 | or die "Could not create $git_tree: $!"; | |
456 | chdir($git_tree); | |
d4f8b390 | 457 | |
a57a9493 | 458 | my $last_branch = ""; |
46541669 | 459 | my $orig_branch = ""; |
a57a9493 | 460 | my %branch_date; |
8a5f2eac | 461 | my $tip_at_start = undef; |
a57a9493 MU |
462 | |
463 | my $git_dir = $ENV{"GIT_DIR"} || ".git"; | |
464 | $git_dir = getwd()."/".$git_dir unless $git_dir =~ m#^/#; | |
465 | $ENV{"GIT_DIR"} = $git_dir; | |
79ee456c SV |
466 | my $orig_git_index; |
467 | $orig_git_index = $ENV{GIT_INDEX_FILE} if exists $ENV{GIT_INDEX_FILE}; | |
8f732649 ML |
468 | |
469 | my %index; # holds filenames of one index per branch | |
061303f0 | 470 | |
a57a9493 MU |
471 | unless(-d $git_dir) { |
472 | system("git-init-db"); | |
473 | die "Cannot init the GIT db at $git_tree: $?\n" if $?; | |
474 | system("git-read-tree"); | |
475 | die "Cannot init an empty tree: $?\n" if $?; | |
476 | ||
477 | $last_branch = $opt_o; | |
46541669 | 478 | $orig_branch = ""; |
a57a9493 | 479 | } else { |
866d1310 | 480 | -f "$git_dir/refs/heads/$opt_o" |
4c24e089 MU |
481 | or die "Branch '$opt_o' does not exist.\n". |
482 | "Either use the correct '-o branch' option,\n". | |
483 | "or import to a new repository.\n"; | |
484 | ||
8366a10a PR |
485 | open(F, "git-symbolic-ref HEAD |") or |
486 | die "Cannot run git-symbolic-ref: $!\n"; | |
487 | chomp ($last_branch = <F>); | |
488 | $last_branch = basename($last_branch); | |
489 | close(F); | |
46541669 MU |
490 | unless($last_branch) { |
491 | warn "Cannot read the last branch name: $! -- assuming 'master'\n"; | |
492 | $last_branch = "master"; | |
493 | } | |
494 | $orig_branch = $last_branch; | |
8a5f2eac | 495 | $tip_at_start = `git-rev-parse --verify HEAD`; |
a57a9493 MU |
496 | |
497 | # Get the last import timestamps | |
498 | opendir(D,"$git_dir/refs/heads"); | |
499 | while(defined(my $head = readdir(D))) { | |
500 | next if $head =~ /^\./; | |
501 | open(F,"$git_dir/refs/heads/$head") | |
502 | or die "Bad head branch: $head: $!\n"; | |
503 | chomp(my $ftag = <F>); | |
504 | close(F); | |
505 | open(F,"git-cat-file commit $ftag |"); | |
506 | while(<F>) { | |
507 | next unless /^author\s.*\s(\d+)\s[-+]\d{4}$/; | |
508 | $branch_date{$head} = $1; | |
509 | last; | |
510 | } | |
511 | close(F); | |
512 | } | |
513 | closedir(D); | |
514 | } | |
515 | ||
516 | -d $git_dir | |
517 | or die "Could not create git subdir ($git_dir).\n"; | |
518 | ||
ffd97f3a AE |
519 | # now we read (and possibly save) author-info as well |
520 | -f "$git_dir/cvs-authors" and | |
521 | read_author_info("$git_dir/cvs-authors"); | |
522 | if ($opt_A) { | |
523 | read_author_info($opt_A); | |
524 | write_author_info("$git_dir/cvs-authors"); | |
525 | } | |
526 | ||
2f57c697 ML |
527 | |
528 | # | |
529 | # run cvsps into a file unless we are getting | |
530 | # it passed as a file via $opt_P | |
531 | # | |
532 | unless ($opt_P) { | |
533 | print "Running cvsps...\n" if $opt_v; | |
534 | my $pid = open(CVSPS,"-|"); | |
535 | die "Cannot fork: $!\n" unless defined $pid; | |
536 | unless($pid) { | |
537 | my @opt; | |
538 | @opt = split(/,/,$opt_p) if defined $opt_p; | |
539 | unshift @opt, '-z', $opt_z if defined $opt_z; | |
540 | unshift @opt, '-q' unless defined $opt_v; | |
541 | unless (defined($opt_p) && $opt_p =~ m/--no-cvs-direct/) { | |
542 | push @opt, '--cvs-direct'; | |
543 | } | |
544 | exec("cvsps","--norc",@opt,"-u","-A",'--root',$opt_d,$cvs_tree); | |
545 | die "Could not start cvsps: $!\n"; | |
df73e9c6 | 546 | } |
2f57c697 ML |
547 | my ($cvspsfh, $cvspsfile) = tempfile('gitXXXXXX', SUFFIX => '.cvsps', |
548 | DIR => File::Spec->tmpdir()); | |
549 | while (<CVSPS>) { | |
550 | print $cvspsfh $_; | |
211dcac6 | 551 | } |
2f57c697 ML |
552 | close CVSPS; |
553 | close $cvspsfh; | |
554 | $opt_P = $cvspsfile; | |
a57a9493 MU |
555 | } |
556 | ||
557 | ||
2f57c697 ML |
558 | open(CVS, "<$opt_P") or die $!; |
559 | ||
a57a9493 MU |
560 | ## cvsps output: |
561 | #--------------------- | |
562 | #PatchSet 314 | |
563 | #Date: 1999/09/18 13:03:59 | |
564 | #Author: wkoch | |
565 | #Branch: STABLE-BRANCH-1-0 | |
566 | #Ancestor branch: HEAD | |
567 | #Tag: (none) | |
568 | #Log: | |
569 | # See ChangeLog: Sat Sep 18 13:03:28 CEST 1999 Werner Koch | |
570 | #Members: | |
571 | # README:1.57->1.57.2.1 | |
572 | # VERSION:1.96->1.96.2.1 | |
573 | # | |
574 | #--------------------- | |
575 | ||
576 | my $state = 0; | |
577 | ||
e73aefe4 JK |
578 | sub update_index (\@\@) { |
579 | my $old = shift; | |
580 | my $new = shift; | |
6a1871e1 JK |
581 | open(my $fh, '|-', qw(git-update-index -z --index-info)) |
582 | or die "unable to open git-update-index: $!"; | |
583 | print $fh | |
584 | (map { "0 0000000000000000000000000000000000000000\t$_\0" } | |
e73aefe4 | 585 | @$old), |
6a1871e1 | 586 | (map { '100' . sprintf('%o', $_->[0]) . " $_->[1]\t$_->[2]\0" } |
e73aefe4 | 587 | @$new) |
6a1871e1 JK |
588 | or die "unable to write to git-update-index: $!"; |
589 | close $fh | |
590 | or die "unable to write to git-update-index: $!"; | |
591 | $? and die "git-update-index reported error: $?"; | |
e73aefe4 | 592 | } |
a57a9493 | 593 | |
e73aefe4 JK |
594 | sub write_tree () { |
595 | open(my $fh, '-|', qw(git-write-tree)) | |
596 | or die "unable to open git-write-tree: $!"; | |
597 | chomp(my $tree = <$fh>); | |
598 | is_sha1($tree) | |
599 | or die "Cannot get tree id ($tree): $!"; | |
600 | close($fh) | |
a57a9493 MU |
601 | or die "Error running git-write-tree: $?\n"; |
602 | print "Tree ID $tree\n" if $opt_v; | |
e73aefe4 JK |
603 | return $tree; |
604 | } | |
a57a9493 | 605 | |
e73aefe4 | 606 | my($patchset,$date,$author_name,$author_email,$branch,$ancestor,$tag,$logmsg); |
71b08148 ML |
607 | my(@old,@new,@skipped,%ignorebranch); |
608 | ||
609 | # commits that cvsps cannot place anywhere... | |
610 | $ignorebranch{'#CVSPS_NO_BRANCH'} = 1; | |
611 | ||
e73aefe4 | 612 | sub commit { |
c5f448b0 ML |
613 | if ($branch eq $opt_o && !$index{branch} && !get_headref($branch, $git_dir)) { |
614 | # looks like an initial commit | |
615 | # use the index primed by git-init-db | |
616 | $ENV{GIT_INDEX_FILE} = '.git/index'; | |
617 | $index{$branch} = '.git/index'; | |
618 | } else { | |
619 | # use an index per branch to speed up | |
620 | # imports of projects with many branches | |
621 | unless ($index{$branch}) { | |
622 | $index{$branch} = tmpnam(); | |
623 | $ENV{GIT_INDEX_FILE} = $index{$branch}; | |
624 | if ($ancestor) { | |
625 | system("git-read-tree", $ancestor); | |
626 | } else { | |
627 | system("git-read-tree", $branch); | |
628 | } | |
629 | die "read-tree failed: $?\n" if $?; | |
630 | } | |
631 | } | |
632 | $ENV{GIT_INDEX_FILE} = $index{$branch}; | |
633 | ||
e73aefe4 JK |
634 | update_index(@old, @new); |
635 | @old = @new = (); | |
636 | my $tree = write_tree(); | |
637 | my $parent = get_headref($last_branch, $git_dir); | |
638 | print "Parent ID " . ($parent ? $parent : "(empty)") . "\n" if $opt_v; | |
639 | ||
640 | my @commit_args; | |
641 | push @commit_args, ("-p", $parent) if $parent; | |
642 | ||
643 | # loose detection of merges | |
644 | # based on the commit msg | |
645 | foreach my $rx (@mergerx) { | |
646 | next unless $logmsg =~ $rx && $1; | |
647 | my $mparent = $1 eq 'HEAD' ? $opt_o : $1; | |
648 | if(my $sha1 = get_headref($mparent, $git_dir)) { | |
649 | push @commit_args, '-p', $mparent; | |
650 | print "Merge parent branch: $mparent\n" if $opt_v; | |
db4b6582 | 651 | } |
a57a9493 | 652 | } |
e73aefe4 JK |
653 | |
654 | my $commit_date = strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)); | |
62bf0d96 JK |
655 | $ENV{GIT_AUTHOR_NAME} = $author_name; |
656 | $ENV{GIT_AUTHOR_EMAIL} = $author_email; | |
657 | $ENV{GIT_AUTHOR_DATE} = $commit_date; | |
658 | $ENV{GIT_COMMITTER_NAME} = $author_name; | |
659 | $ENV{GIT_COMMITTER_EMAIL} = $author_email; | |
660 | $ENV{GIT_COMMITTER_DATE} = $commit_date; | |
e73aefe4 | 661 | my $pid = open2(my $commit_read, my $commit_write, |
e73aefe4 | 662 | 'git-commit-tree', $tree, @commit_args); |
e371046b MU |
663 | |
664 | # compatibility with git2cvs | |
665 | substr($logmsg,32767) = "" if length($logmsg) > 32767; | |
666 | $logmsg =~ s/[\s\n]+\z//; | |
667 | ||
5179c8a5 ML |
668 | if (@skipped) { |
669 | $logmsg .= "\n\n\nSKIPPED:\n\t"; | |
670 | $logmsg .= join("\n\t", @skipped) . "\n"; | |
f396f01f | 671 | @skipped = (); |
5179c8a5 ML |
672 | } |
673 | ||
e73aefe4 | 674 | print($commit_write "$logmsg\n") && close($commit_write) |
a57a9493 | 675 | or die "Error writing to git-commit-tree: $!\n"; |
2a3e1a85 | 676 | |
e73aefe4 JK |
677 | print "Committed patch $patchset ($branch $commit_date)\n" if $opt_v; |
678 | chomp(my $cid = <$commit_read>); | |
679 | is_sha1($cid) or die "Cannot get commit id ($cid): $!\n"; | |
a57a9493 | 680 | print "Commit ID $cid\n" if $opt_v; |
e73aefe4 | 681 | close($commit_read); |
2a3e1a85 MU |
682 | |
683 | waitpid($pid,0); | |
684 | die "Error running git-commit-tree: $?\n" if $?; | |
a57a9493 | 685 | |
42277bc8 | 686 | system("git-update-ref refs/heads/$branch $cid") == 0 |
a57a9493 MU |
687 | or die "Cannot write branch $branch for update: $!\n"; |
688 | ||
689 | if($tag) { | |
0d821d4d PA |
690 | my($in, $out) = ('',''); |
691 | my($xtag) = $tag; | |
692 | $xtag =~ s/\s+\*\*.*$//; # Remove stuff like ** INVALID ** and ** FUNKY ** | |
693 | $xtag =~ tr/_/\./ if ( $opt_u ); | |
34c99da2 | 694 | $xtag =~ s/[\/]/$opt_s/g; |
0d821d4d PA |
695 | |
696 | my $pid = open2($in, $out, 'git-mktag'); | |
697 | print $out "object $cid\n". | |
698 | "type commit\n". | |
699 | "tag $xtag\n". | |
94c23343 | 700 | "tagger $author_name <$author_email>\n" |
0d821d4d PA |
701 | or die "Cannot create tag object $xtag: $!\n"; |
702 | close($out) | |
703 | or die "Cannot create tag object $xtag: $!\n"; | |
704 | ||
705 | my $tagobj = <$in>; | |
706 | chomp $tagobj; | |
707 | ||
708 | if ( !close($in) or waitpid($pid, 0) != $pid or | |
709 | $? != 0 or $tagobj !~ /^[0123456789abcdef]{40}$/ ) { | |
710 | die "Cannot create tag object $xtag: $!\n"; | |
711 | } | |
712 | ||
713 | ||
714 | open(C,">$git_dir/refs/tags/$xtag") | |
715 | or die "Cannot create tag $xtag: $!\n"; | |
716 | print C "$tagobj\n" | |
717 | or die "Cannot write tag $xtag: $!\n"; | |
a57a9493 | 718 | close(C) |
0d821d4d PA |
719 | or die "Cannot write tag $xtag: $!\n"; |
720 | ||
721 | print "Created tag '$xtag' on '$branch'\n" if $opt_v; | |
a57a9493 | 722 | } |
a57a9493 MU |
723 | }; |
724 | ||
06918348 | 725 | my $commitcount = 1; |
a57a9493 MU |
726 | while(<CVS>) { |
727 | chomp; | |
728 | if($state == 0 and /^-+$/) { | |
729 | $state = 1; | |
730 | } elsif($state == 0) { | |
731 | $state = 1; | |
732 | redo; | |
733 | } elsif(($state==0 or $state==1) and s/^PatchSet\s+//) { | |
734 | $patchset = 0+$_; | |
735 | $state=2; | |
736 | } elsif($state == 2 and s/^Date:\s+//) { | |
737 | $date = pdate($_); | |
738 | unless($date) { | |
739 | print STDERR "Could not parse date: $_\n"; | |
740 | $state=0; | |
741 | next; | |
742 | } | |
743 | $state=3; | |
744 | } elsif($state == 3 and s/^Author:\s+//) { | |
745 | s/\s+$//; | |
94c23343 JH |
746 | if (/^(.*?)\s+<(.*)>/) { |
747 | ($author_name, $author_email) = ($1, $2); | |
ffd97f3a AE |
748 | } elsif ($conv_author_name{$_}) { |
749 | $author_name = $conv_author_name{$_}; | |
750 | $author_email = $conv_author_email{$_}; | |
94c23343 JH |
751 | } else { |
752 | $author_name = $author_email = $_; | |
753 | } | |
a57a9493 MU |
754 | $state = 4; |
755 | } elsif($state == 4 and s/^Branch:\s+//) { | |
756 | s/\s+$//; | |
fbfd60d6 | 757 | s/[\/]/$opt_s/g; |
a57a9493 MU |
758 | $branch = $_; |
759 | $state = 5; | |
760 | } elsif($state == 5 and s/^Ancestor branch:\s+//) { | |
761 | s/\s+$//; | |
762 | $ancestor = $_; | |
0fa2824f | 763 | $ancestor = $opt_o if $ancestor eq "HEAD"; |
a57a9493 MU |
764 | $state = 6; |
765 | } elsif($state == 5) { | |
766 | $ancestor = undef; | |
767 | $state = 6; | |
768 | redo; | |
769 | } elsif($state == 6 and s/^Tag:\s+//) { | |
770 | s/\s+$//; | |
771 | if($_ eq "(none)") { | |
772 | $tag = undef; | |
773 | } else { | |
774 | $tag = $_; | |
775 | } | |
776 | $state = 7; | |
777 | } elsif($state == 7 and /^Log:/) { | |
778 | $logmsg = ""; | |
779 | $state = 8; | |
780 | } elsif($state == 8 and /^Members:/) { | |
781 | $branch = $opt_o if $branch eq "HEAD"; | |
782 | if(defined $branch_date{$branch} and $branch_date{$branch} >= $date) { | |
783 | # skip | |
9da07f34 | 784 | print "skip patchset $patchset: $date before $branch_date{$branch}\n" if $opt_v; |
a57a9493 MU |
785 | $state = 11; |
786 | next; | |
787 | } | |
71b08148 ML |
788 | if (exists $ignorebranch{$branch}) { |
789 | print STDERR "Skipping $branch\n"; | |
790 | $state = 11; | |
791 | next; | |
792 | } | |
a57a9493 | 793 | if($ancestor) { |
71b08148 ML |
794 | if($ancestor eq $branch) { |
795 | print STDERR "Branch $branch erroneously stems from itself -- changed ancestor to $opt_o\n"; | |
796 | $ancestor = $opt_o; | |
797 | } | |
a57a9493 MU |
798 | if(-f "$git_dir/refs/heads/$branch") { |
799 | print STDERR "Branch $branch already exists!\n"; | |
800 | $state=11; | |
801 | next; | |
802 | } | |
803 | unless(open(H,"$git_dir/refs/heads/$ancestor")) { | |
804 | print STDERR "Branch $ancestor does not exist!\n"; | |
71b08148 | 805 | $ignorebranch{$branch} = 1; |
a57a9493 MU |
806 | $state=11; |
807 | next; | |
808 | } | |
809 | chomp(my $id = <H>); | |
810 | close(H); | |
811 | unless(open(H,"> $git_dir/refs/heads/$branch")) { | |
812 | print STDERR "Could not create branch $branch: $!\n"; | |
71b08148 | 813 | $ignorebranch{$branch} = 1; |
a57a9493 MU |
814 | $state=11; |
815 | next; | |
816 | } | |
817 | print H "$id\n" | |
818 | or die "Could not write branch $branch: $!"; | |
819 | close(H) | |
820 | or die "Could not write branch $branch: $!"; | |
821 | } | |
46e63efc | 822 | $last_branch = $branch if $branch ne $last_branch; |
a57a9493 MU |
823 | $state = 9; |
824 | } elsif($state == 8) { | |
825 | $logmsg .= "$_\n"; | |
8b8840e0 | 826 | } elsif($state == 9 and /^\s+(.+?):(INITIAL|\d+(?:\.\d+)+)->(\d+(?:\.\d+)+)\s*$/) { |
a57a9493 | 827 | # VERSION:1.96->1.96.2.1 |
2a3e1a85 | 828 | my $init = ($2 eq "INITIAL"); |
a57a9493 | 829 | my $fn = $1; |
f65ae603 MU |
830 | my $rev = $3; |
831 | $fn =~ s#^/+##; | |
5179c8a5 ML |
832 | if ($opt_S && $fn =~ m/$opt_S/) { |
833 | print "SKIPPING $fn v $rev\n"; | |
834 | push(@skipped, $fn); | |
835 | next; | |
836 | } | |
837 | print "Fetching $fn v $rev\n" if $opt_v; | |
2eb6d82e | 838 | my ($tmpname, $size) = $cvs->file($fn,$rev); |
8b8840e0 MU |
839 | if($size == -1) { |
840 | push(@old,$fn); | |
841 | print "Drop $fn\n" if $opt_v; | |
842 | } else { | |
843 | print "".($init ? "New" : "Update")." $fn: $size bytes\n" if $opt_v; | |
dd27478f JH |
844 | my $pid = open(my $F, '-|'); |
845 | die $! unless defined $pid; | |
846 | if (!$pid) { | |
847 | exec("git-hash-object", "-w", $tmpname) | |
8b8840e0 | 848 | or die "Cannot create object: $!\n"; |
dd27478f | 849 | } |
8b8840e0 MU |
850 | my $sha = <$F>; |
851 | chomp $sha; | |
852 | close $F; | |
853 | my $mode = pmode($cvs->{'mode'}); | |
854 | push(@new,[$mode, $sha, $fn]); # may be resurrected! | |
855 | } | |
2eb6d82e | 856 | unlink($tmpname); |
b0921331 | 857 | } elsif($state == 9 and /^\s+(.+?):\d+(?:\.\d+)+->(\d+(?:\.\d+)+)\(DEAD\)\s*$/) { |
f65ae603 MU |
858 | my $fn = $1; |
859 | $fn =~ s#^/+##; | |
860 | push(@old,$fn); | |
8b8840e0 | 861 | print "Delete $fn\n" if $opt_v; |
a57a9493 MU |
862 | } elsif($state == 9 and /^\s*$/) { |
863 | $state = 10; | |
864 | } elsif(($state == 9 or $state == 10) and /^-+$/) { | |
4adcea99 LT |
865 | $commitcount++; |
866 | if ($opt_L && $commitcount > $opt_L) { | |
06918348 ML |
867 | last; |
868 | } | |
c4b16f8d | 869 | commit(); |
4adcea99 LT |
870 | if (($commitcount & 1023) == 0) { |
871 | system("git repack -a -d"); | |
872 | } | |
a57a9493 MU |
873 | $state = 1; |
874 | } elsif($state == 11 and /^-+$/) { | |
875 | $state = 1; | |
876 | } elsif(/^-+$/) { # end of unknown-line processing | |
877 | $state = 1; | |
878 | } elsif($state != 11) { # ignore stuff when skipping | |
879 | print "* UNKNOWN LINE * $_\n"; | |
880 | } | |
881 | } | |
c4b16f8d | 882 | commit() if $branch and $state != 11; |
d4f8b390 | 883 | |
8f732649 | 884 | foreach my $git_index (values %index) { |
c5f448b0 ML |
885 | if ($git_index ne '.git/index') { |
886 | unlink($git_index); | |
887 | } | |
8f732649 | 888 | } |
79ee456c | 889 | |
210569f9 SV |
890 | if (defined $orig_git_index) { |
891 | $ENV{GIT_INDEX_FILE} = $orig_git_index; | |
892 | } else { | |
893 | delete $ENV{GIT_INDEX_FILE}; | |
894 | } | |
895 | ||
46541669 MU |
896 | # Now switch back to the branch we were in before all of this happened |
897 | if($orig_branch) { | |
8a5f2eac JH |
898 | print "DONE.\n" if $opt_v; |
899 | if ($opt_i) { | |
900 | exit 0; | |
901 | } | |
902 | my $tip_at_end = `git-rev-parse --verify HEAD`; | |
903 | if ($tip_at_start ne $tip_at_end) { | |
cb9594e2 | 904 | for ($tip_at_start, $tip_at_end) { chomp; } |
8a5f2eac JH |
905 | print "Fetched into the current branch.\n" if $opt_v; |
906 | system(qw(git-read-tree -u -m), | |
907 | $tip_at_start, $tip_at_end); | |
908 | die "Fast-forward update failed: $?\n" if $?; | |
909 | } | |
910 | else { | |
911 | system(qw(git-merge cvsimport HEAD), "refs/heads/$opt_o"); | |
912 | die "Could not merge $opt_o into the current branch.\n" if $?; | |
913 | } | |
46541669 MU |
914 | } else { |
915 | $orig_branch = "master"; | |
916 | print "DONE; creating $orig_branch branch\n" if $opt_v; | |
a541211e | 917 | system("git-update-ref", "refs/heads/master", "refs/heads/$opt_o") |
46541669 | 918 | unless -f "$git_dir/refs/heads/master"; |
8366a10a | 919 | system('git-update-ref', 'HEAD', "$orig_branch"); |
c1c774e7 SV |
920 | unless ($opt_i) { |
921 | system('git checkout'); | |
922 | die "checkout failed: $?\n" if $?; | |
923 | } | |
46541669 | 924 | } |