]>
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 SV |
19 | use File::Spec; |
20 | use File::Temp qw(tempfile); | |
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; | |
26 | use POSIX qw(strftime dup2); | |
0d821d4d | 27 | use IPC::Open2; |
a57a9493 MU |
28 | |
29 | $SIG{'PIPE'}="IGNORE"; | |
30 | $ENV{'TZ'}="UTC"; | |
31 | ||
ffd97f3a AE |
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); |
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] | |
40 | [-s subst] [-m] [-M 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 | ||
88 | getopts("hivmkuo:d:p:C:z:s:M:P:A:") 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=""; | |
2eb6d82e | 318 | $res=0; |
a57a9493 MU |
319 | while($cnt) { |
320 | my $buf; | |
321 | my $num = $self->{'socketi'}->read($buf,$cnt); | |
322 | die "Server: Filesize $cnt: $num: $!\n" if not defined $num or $num<=0; | |
2eb6d82e SV |
323 | print $fh $buf; |
324 | $res += $num; | |
a57a9493 MU |
325 | $cnt -= $num; |
326 | } | |
327 | } elsif($line =~ s/^ //) { | |
2eb6d82e SV |
328 | print $fh $line; |
329 | $res += length($line); | |
a57a9493 MU |
330 | } elsif($line =~ /^M\b/) { |
331 | # output, do nothing | |
332 | } elsif($line =~ /^Mbinary\b/) { | |
333 | my $cnt; | |
334 | die "EOF from server after 'Mbinary'" unless defined ($cnt = $self->readline()); | |
335 | chomp $cnt; | |
336 | die "Duh: Mbinary $cnt" if $cnt !~ /^\d+$/ or $cnt<1; | |
337 | $line=""; | |
338 | while($cnt) { | |
339 | my $buf; | |
340 | my $num = $self->{'socketi'}->read($buf,$cnt); | |
341 | die "S: Mbinary $cnt: $num: $!\n" if not defined $num or $num<=0; | |
2eb6d82e SV |
342 | print $fh $buf; |
343 | $res += $num; | |
a57a9493 MU |
344 | $cnt -= $num; |
345 | } | |
346 | } else { | |
347 | chomp $line; | |
348 | if($line eq "ok") { | |
349 | # print STDERR "S: ok (".length($res).")\n"; | |
350 | return $res; | |
351 | } elsif($line =~ s/^E //) { | |
352 | # print STDERR "S: $line\n"; | |
8b8840e0 MU |
353 | } elsif($line =~ /^Remove-entry /i) { |
354 | $line = $self->readline(); # filename | |
355 | $line = $self->readline(); # OK | |
356 | chomp $line; | |
357 | die "Unknown: $line" if $line ne "ok"; | |
358 | return -1; | |
a57a9493 MU |
359 | } else { |
360 | die "Unknown: $line\n"; | |
361 | } | |
362 | } | |
363 | } | |
364 | } | |
365 | sub file { | |
366 | my($self,$fn,$rev) = @_; | |
367 | my $res; | |
368 | ||
2eb6d82e SV |
369 | my ($fh, $name) = tempfile('gitcvs.XXXXXX', |
370 | DIR => File::Spec->tmpdir(), UNLINK => 1); | |
371 | ||
372 | $self->_file($fn,$rev) and $res = $self->_line($fh); | |
373 | ||
374 | if (!defined $res) { | |
375 | # retry | |
376 | $self->conn(); | |
377 | $self->_file($fn,$rev) | |
378 | or die "No file command send\n"; | |
379 | $res = $self->_line($fh); | |
380 | die "No input: $fn $rev\n" unless defined $res; | |
a57a9493 | 381 | } |
c619ad51 | 382 | close ($fh); |
a57a9493 | 383 | |
d44e8cf0 ML |
384 | if ($res eq '') { |
385 | die "Looks like the server has gone away while fetching $fn $rev -- exiting!"; | |
386 | } | |
387 | ||
2eb6d82e | 388 | return ($name, $res); |
a57a9493 MU |
389 | } |
390 | ||
391 | ||
392 | package main; | |
393 | ||
2a3e1a85 | 394 | my $cvs = CVSconn->new($opt_d, $cvs_tree); |
a57a9493 MU |
395 | |
396 | ||
397 | sub pdate($) { | |
398 | my($d) = @_; | |
399 | m#(\d{2,4})/(\d\d)/(\d\d)\s(\d\d):(\d\d)(?::(\d\d))?# | |
400 | or die "Unparseable date: $d\n"; | |
401 | my $y=$1; $y-=1900 if $y>1900; | |
402 | return timegm($6||0,$5,$4,$3,$2-1,$y); | |
9718a00b TM |
403 | } |
404 | ||
a57a9493 MU |
405 | sub pmode($) { |
406 | my($mode) = @_; | |
407 | my $m = 0; | |
408 | my $mm = 0; | |
409 | my $um = 0; | |
410 | for my $x(split(//,$mode)) { | |
411 | if($x eq ",") { | |
412 | $m |= $mm&$um; | |
413 | $mm = 0; | |
414 | $um = 0; | |
415 | } elsif($x eq "u") { $um |= 0700; | |
416 | } elsif($x eq "g") { $um |= 0070; | |
417 | } elsif($x eq "o") { $um |= 0007; | |
418 | } elsif($x eq "r") { $mm |= 0444; | |
419 | } elsif($x eq "w") { $mm |= 0222; | |
420 | } elsif($x eq "x") { $mm |= 0111; | |
421 | } elsif($x eq "=") { # do nothing | |
422 | } else { die "Unknown mode: $mode\n"; | |
423 | } | |
424 | } | |
425 | $m |= $mm&$um; | |
426 | return $m; | |
427 | } | |
d4f8b390 | 428 | |
a57a9493 MU |
429 | sub getwd() { |
430 | my $pwd = `pwd`; | |
431 | chomp $pwd; | |
432 | return $pwd; | |
d4f8b390 LT |
433 | } |
434 | ||
db4b6582 ML |
435 | |
436 | sub get_headref($$) { | |
437 | my $name = shift; | |
438 | my $git_dir = shift; | |
439 | my $sha; | |
440 | ||
441 | if (open(C,"$git_dir/refs/heads/$name")) { | |
442 | chomp($sha = <C>); | |
443 | close(C); | |
444 | length($sha) == 40 | |
445 | or die "Cannot get head id for $name ($sha): $!\n"; | |
446 | } | |
447 | return $sha; | |
448 | } | |
449 | ||
450 | ||
a57a9493 MU |
451 | -d $git_tree |
452 | or mkdir($git_tree,0777) | |
453 | or die "Could not create $git_tree: $!"; | |
454 | chdir($git_tree); | |
d4f8b390 | 455 | |
a57a9493 | 456 | my $last_branch = ""; |
46541669 | 457 | my $orig_branch = ""; |
210569f9 | 458 | my $forward_master = 0; |
a57a9493 MU |
459 | my %branch_date; |
460 | ||
461 | my $git_dir = $ENV{"GIT_DIR"} || ".git"; | |
462 | $git_dir = getwd()."/".$git_dir unless $git_dir =~ m#^/#; | |
463 | $ENV{"GIT_DIR"} = $git_dir; | |
79ee456c SV |
464 | my $orig_git_index; |
465 | $orig_git_index = $ENV{GIT_INDEX_FILE} if exists $ENV{GIT_INDEX_FILE}; | |
466 | my ($git_ih, $git_index) = tempfile('gitXXXXXX', SUFFIX => '.idx', | |
467 | DIR => File::Spec->tmpdir()); | |
468 | close ($git_ih); | |
469 | $ENV{GIT_INDEX_FILE} = $git_index; | |
a57a9493 MU |
470 | unless(-d $git_dir) { |
471 | system("git-init-db"); | |
472 | die "Cannot init the GIT db at $git_tree: $?\n" if $?; | |
473 | system("git-read-tree"); | |
474 | die "Cannot init an empty tree: $?\n" if $?; | |
475 | ||
476 | $last_branch = $opt_o; | |
46541669 | 477 | $orig_branch = ""; |
a57a9493 | 478 | } else { |
866d1310 | 479 | -f "$git_dir/refs/heads/$opt_o" |
4c24e089 MU |
480 | or die "Branch '$opt_o' does not exist.\n". |
481 | "Either use the correct '-o branch' option,\n". | |
482 | "or import to a new repository.\n"; | |
483 | ||
8366a10a PR |
484 | open(F, "git-symbolic-ref HEAD |") or |
485 | die "Cannot run git-symbolic-ref: $!\n"; | |
486 | chomp ($last_branch = <F>); | |
487 | $last_branch = basename($last_branch); | |
488 | close(F); | |
46541669 MU |
489 | unless($last_branch) { |
490 | warn "Cannot read the last branch name: $! -- assuming 'master'\n"; | |
491 | $last_branch = "master"; | |
492 | } | |
493 | $orig_branch = $last_branch; | |
210569f9 SV |
494 | if (-f "$git_dir/CVS2GIT_HEAD") { |
495 | die <<EOM; | |
496 | CVS2GIT_HEAD exists. | |
497 | Make sure your working directory corresponds to HEAD and remove CVS2GIT_HEAD. | |
498 | You may need to run | |
499 | ||
ffd97f3a | 500 | git read-tree -m -u CVS2GIT_HEAD HEAD |
210569f9 SV |
501 | EOM |
502 | } | |
503 | system('cp', "$git_dir/HEAD", "$git_dir/CVS2GIT_HEAD"); | |
504 | ||
505 | $forward_master = | |
506 | $opt_o ne 'master' && -f "$git_dir/refs/heads/master" && | |
507 | system('cmp', '-s', "$git_dir/refs/heads/master", | |
508 | "$git_dir/refs/heads/$opt_o") == 0; | |
a57a9493 | 509 | |
79ee456c SV |
510 | # populate index |
511 | system('git-read-tree', $last_branch); | |
17501132 | 512 | die "read-tree failed: $?\n" if $?; |
a57a9493 MU |
513 | |
514 | # Get the last import timestamps | |
515 | opendir(D,"$git_dir/refs/heads"); | |
516 | while(defined(my $head = readdir(D))) { | |
517 | next if $head =~ /^\./; | |
518 | open(F,"$git_dir/refs/heads/$head") | |
519 | or die "Bad head branch: $head: $!\n"; | |
520 | chomp(my $ftag = <F>); | |
521 | close(F); | |
522 | open(F,"git-cat-file commit $ftag |"); | |
523 | while(<F>) { | |
524 | next unless /^author\s.*\s(\d+)\s[-+]\d{4}$/; | |
525 | $branch_date{$head} = $1; | |
526 | last; | |
527 | } | |
528 | close(F); | |
529 | } | |
530 | closedir(D); | |
531 | } | |
532 | ||
533 | -d $git_dir | |
534 | or die "Could not create git subdir ($git_dir).\n"; | |
535 | ||
ffd97f3a AE |
536 | # now we read (and possibly save) author-info as well |
537 | -f "$git_dir/cvs-authors" and | |
538 | read_author_info("$git_dir/cvs-authors"); | |
539 | if ($opt_A) { | |
540 | read_author_info($opt_A); | |
541 | write_author_info("$git_dir/cvs-authors"); | |
542 | } | |
543 | ||
a57a9493 MU |
544 | my $pid = open(CVS,"-|"); |
545 | die "Cannot fork: $!\n" unless defined $pid; | |
546 | unless($pid) { | |
2be4fcc3 MU |
547 | my @opt; |
548 | @opt = split(/,/,$opt_p) if defined $opt_p; | |
28537171 | 549 | unshift @opt, '-z', $opt_z if defined $opt_z; |
9acb552d | 550 | unshift @opt, '-q' unless defined $opt_v; |
e1757689 | 551 | unless (defined($opt_p) && $opt_p =~ m/--no-cvs-direct/) { |
df73e9c6 ML |
552 | push @opt, '--cvs-direct'; |
553 | } | |
211dcac6 ML |
554 | if ($opt_P) { |
555 | exec("cat", $opt_P); | |
556 | } else { | |
2c52a42d | 557 | exec("cvsps","--norc",@opt,"-u","-A",'--root',$opt_d,$cvs_tree); |
211dcac6 ML |
558 | die "Could not start cvsps: $!\n"; |
559 | } | |
a57a9493 MU |
560 | } |
561 | ||
562 | ||
563 | ## cvsps output: | |
564 | #--------------------- | |
565 | #PatchSet 314 | |
566 | #Date: 1999/09/18 13:03:59 | |
567 | #Author: wkoch | |
568 | #Branch: STABLE-BRANCH-1-0 | |
569 | #Ancestor branch: HEAD | |
570 | #Tag: (none) | |
571 | #Log: | |
572 | # See ChangeLog: Sat Sep 18 13:03:28 CEST 1999 Werner Koch | |
573 | #Members: | |
574 | # README:1.57->1.57.2.1 | |
575 | # VERSION:1.96->1.96.2.1 | |
576 | # | |
577 | #--------------------- | |
578 | ||
579 | my $state = 0; | |
580 | ||
94c23343 | 581 | my($patchset,$date,$author_name,$author_email,$branch,$ancestor,$tag,$logmsg); |
a57a9493 MU |
582 | my(@old,@new); |
583 | my $commit = sub { | |
584 | my $pid; | |
4abdecbf MU |
585 | while(@old) { |
586 | my @o2; | |
587 | if(@old > 55) { | |
588 | @o2 = splice(@old,0,50); | |
589 | } else { | |
590 | @o2 = @old; | |
591 | @old = (); | |
592 | } | |
215a7ad1 | 593 | system("git-update-index","--force-remove","--",@o2); |
4abdecbf MU |
594 | die "Cannot remove files: $?\n" if $?; |
595 | } | |
596 | while(@new) { | |
597 | my @n2; | |
2eb6d82e SV |
598 | if(@new > 12) { |
599 | @n2 = splice(@new,0,10); | |
4abdecbf MU |
600 | } else { |
601 | @n2 = @new; | |
602 | @new = (); | |
603 | } | |
215a7ad1 | 604 | system("git-update-index","--add", |
2eb6d82e | 605 | (map { ('--cacheinfo', @$_) } @n2)); |
4abdecbf MU |
606 | die "Cannot add files: $?\n" if $?; |
607 | } | |
a57a9493 MU |
608 | |
609 | $pid = open(C,"-|"); | |
610 | die "Cannot fork: $!" unless defined $pid; | |
611 | unless($pid) { | |
612 | exec("git-write-tree"); | |
613 | die "Cannot exec git-write-tree: $!\n"; | |
614 | } | |
615 | chomp(my $tree = <C>); | |
616 | length($tree) == 40 | |
617 | or die "Cannot get tree id ($tree): $!\n"; | |
618 | close(C) | |
619 | or die "Error running git-write-tree: $?\n"; | |
620 | print "Tree ID $tree\n" if $opt_v; | |
621 | ||
622 | my $parent = ""; | |
623 | if(open(C,"$git_dir/refs/heads/$last_branch")) { | |
624 | chomp($parent = <C>); | |
625 | close(C); | |
626 | length($parent) == 40 | |
627 | or die "Cannot get parent id ($parent): $!\n"; | |
628 | print "Parent ID $parent\n" if $opt_v; | |
629 | } | |
630 | ||
17501132 SV |
631 | my $pr = IO::Pipe->new() or die "Cannot open pipe: $!\n"; |
632 | my $pw = IO::Pipe->new() or die "Cannot open pipe: $!\n"; | |
2a3e1a85 MU |
633 | $pid = fork(); |
634 | die "Fork: $!\n" unless defined $pid; | |
a57a9493 | 635 | unless($pid) { |
2a3e1a85 MU |
636 | $pr->writer(); |
637 | $pw->reader(); | |
89764f5d | 638 | open(OUT,">&STDOUT"); |
2a3e1a85 MU |
639 | dup2($pw->fileno(),0); |
640 | dup2($pr->fileno(),1); | |
641 | $pr->close(); | |
642 | $pw->close(); | |
643 | ||
a57a9493 MU |
644 | my @par = (); |
645 | @par = ("-p",$parent) if $parent; | |
db4b6582 ML |
646 | |
647 | # loose detection of merges | |
648 | # based on the commit msg | |
649 | foreach my $rx (@mergerx) { | |
650 | if ($logmsg =~ $rx) { | |
651 | my $mparent = $1; | |
049f28c3 | 652 | if ($mparent eq 'HEAD') { $mparent = $opt_o }; |
db4b6582 ML |
653 | if ( -e "$git_dir/refs/heads/$mparent") { |
654 | $mparent = get_headref($mparent, $git_dir); | |
655 | push @par, '-p', $mparent; | |
89764f5d | 656 | print OUT "Merge parent branch: $mparent\n" if $opt_v; |
db4b6582 | 657 | } |
29504118 | 658 | } |
db4b6582 ML |
659 | } |
660 | ||
a57a9493 | 661 | exec("env", |
94c23343 JH |
662 | "GIT_AUTHOR_NAME=$author_name", |
663 | "GIT_AUTHOR_EMAIL=$author_email", | |
a57a9493 | 664 | "GIT_AUTHOR_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)), |
94c23343 JH |
665 | "GIT_COMMITTER_NAME=$author_name", |
666 | "GIT_COMMITTER_EMAIL=$author_email", | |
a57a9493 MU |
667 | "GIT_COMMITTER_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)), |
668 | "git-commit-tree", $tree,@par); | |
669 | die "Cannot exec git-commit-tree: $!\n"; | |
670 | } | |
2a3e1a85 MU |
671 | $pw->writer(); |
672 | $pr->reader(); | |
e371046b MU |
673 | |
674 | # compatibility with git2cvs | |
675 | substr($logmsg,32767) = "" if length($logmsg) > 32767; | |
676 | $logmsg =~ s/[\s\n]+\z//; | |
677 | ||
678 | print $pw "$logmsg\n" | |
a57a9493 | 679 | or die "Error writing to git-commit-tree: $!\n"; |
2a3e1a85 MU |
680 | $pw->close(); |
681 | ||
8b8840e0 | 682 | print "Committed patch $patchset ($branch ".strftime("%Y-%m-%d %H:%M:%S",gmtime($date)).")\n" if $opt_v; |
2a3e1a85 | 683 | chomp(my $cid = <$pr>); |
a57a9493 MU |
684 | length($cid) == 40 |
685 | or die "Cannot get commit id ($cid): $!\n"; | |
686 | print "Commit ID $cid\n" if $opt_v; | |
2a3e1a85 MU |
687 | $pr->close(); |
688 | ||
689 | waitpid($pid,0); | |
690 | die "Error running git-commit-tree: $?\n" if $?; | |
a57a9493 MU |
691 | |
692 | open(C,">$git_dir/refs/heads/$branch") | |
693 | or die "Cannot open branch $branch for update: $!\n"; | |
694 | print C "$cid\n" | |
695 | or die "Cannot write branch $branch for update: $!\n"; | |
696 | close(C) | |
697 | or die "Cannot write branch $branch for update: $!\n"; | |
698 | ||
699 | if($tag) { | |
0d821d4d PA |
700 | my($in, $out) = ('',''); |
701 | my($xtag) = $tag; | |
702 | $xtag =~ s/\s+\*\*.*$//; # Remove stuff like ** INVALID ** and ** FUNKY ** | |
703 | $xtag =~ tr/_/\./ if ( $opt_u ); | |
34c99da2 | 704 | $xtag =~ s/[\/]/$opt_s/g; |
0d821d4d PA |
705 | |
706 | my $pid = open2($in, $out, 'git-mktag'); | |
707 | print $out "object $cid\n". | |
708 | "type commit\n". | |
709 | "tag $xtag\n". | |
94c23343 | 710 | "tagger $author_name <$author_email>\n" |
0d821d4d PA |
711 | or die "Cannot create tag object $xtag: $!\n"; |
712 | close($out) | |
713 | or die "Cannot create tag object $xtag: $!\n"; | |
714 | ||
715 | my $tagobj = <$in>; | |
716 | chomp $tagobj; | |
717 | ||
718 | if ( !close($in) or waitpid($pid, 0) != $pid or | |
719 | $? != 0 or $tagobj !~ /^[0123456789abcdef]{40}$/ ) { | |
720 | die "Cannot create tag object $xtag: $!\n"; | |
721 | } | |
722 | ||
723 | ||
724 | open(C,">$git_dir/refs/tags/$xtag") | |
725 | or die "Cannot create tag $xtag: $!\n"; | |
726 | print C "$tagobj\n" | |
727 | or die "Cannot write tag $xtag: $!\n"; | |
a57a9493 | 728 | close(C) |
0d821d4d PA |
729 | or die "Cannot write tag $xtag: $!\n"; |
730 | ||
731 | print "Created tag '$xtag' on '$branch'\n" if $opt_v; | |
a57a9493 | 732 | } |
a57a9493 MU |
733 | }; |
734 | ||
735 | while(<CVS>) { | |
736 | chomp; | |
737 | if($state == 0 and /^-+$/) { | |
738 | $state = 1; | |
739 | } elsif($state == 0) { | |
740 | $state = 1; | |
741 | redo; | |
742 | } elsif(($state==0 or $state==1) and s/^PatchSet\s+//) { | |
743 | $patchset = 0+$_; | |
744 | $state=2; | |
745 | } elsif($state == 2 and s/^Date:\s+//) { | |
746 | $date = pdate($_); | |
747 | unless($date) { | |
748 | print STDERR "Could not parse date: $_\n"; | |
749 | $state=0; | |
750 | next; | |
751 | } | |
752 | $state=3; | |
753 | } elsif($state == 3 and s/^Author:\s+//) { | |
754 | s/\s+$//; | |
94c23343 JH |
755 | if (/^(.*?)\s+<(.*)>/) { |
756 | ($author_name, $author_email) = ($1, $2); | |
ffd97f3a AE |
757 | } elsif ($conv_author_name{$_}) { |
758 | $author_name = $conv_author_name{$_}; | |
759 | $author_email = $conv_author_email{$_}; | |
94c23343 JH |
760 | } else { |
761 | $author_name = $author_email = $_; | |
762 | } | |
a57a9493 MU |
763 | $state = 4; |
764 | } elsif($state == 4 and s/^Branch:\s+//) { | |
765 | s/\s+$//; | |
fbfd60d6 | 766 | s/[\/]/$opt_s/g; |
a57a9493 MU |
767 | $branch = $_; |
768 | $state = 5; | |
769 | } elsif($state == 5 and s/^Ancestor branch:\s+//) { | |
770 | s/\s+$//; | |
771 | $ancestor = $_; | |
0fa2824f | 772 | $ancestor = $opt_o if $ancestor eq "HEAD"; |
a57a9493 MU |
773 | $state = 6; |
774 | } elsif($state == 5) { | |
775 | $ancestor = undef; | |
776 | $state = 6; | |
777 | redo; | |
778 | } elsif($state == 6 and s/^Tag:\s+//) { | |
779 | s/\s+$//; | |
780 | if($_ eq "(none)") { | |
781 | $tag = undef; | |
782 | } else { | |
783 | $tag = $_; | |
784 | } | |
785 | $state = 7; | |
786 | } elsif($state == 7 and /^Log:/) { | |
787 | $logmsg = ""; | |
788 | $state = 8; | |
789 | } elsif($state == 8 and /^Members:/) { | |
790 | $branch = $opt_o if $branch eq "HEAD"; | |
791 | if(defined $branch_date{$branch} and $branch_date{$branch} >= $date) { | |
792 | # skip | |
9da07f34 | 793 | print "skip patchset $patchset: $date before $branch_date{$branch}\n" if $opt_v; |
a57a9493 MU |
794 | $state = 11; |
795 | next; | |
796 | } | |
797 | if($ancestor) { | |
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"; | |
805 | $state=11; | |
806 | next; | |
807 | } | |
808 | chomp(my $id = <H>); | |
809 | close(H); | |
810 | unless(open(H,"> $git_dir/refs/heads/$branch")) { | |
811 | print STDERR "Could not create branch $branch: $!\n"; | |
812 | $state=11; | |
813 | next; | |
814 | } | |
815 | print H "$id\n" | |
816 | or die "Could not write branch $branch: $!"; | |
817 | close(H) | |
818 | or die "Could not write branch $branch: $!"; | |
819 | } | |
820 | if(($ancestor || $branch) ne $last_branch) { | |
2a3e1a85 | 821 | print "Switching from $last_branch to $branch\n" if $opt_v; |
46e63efc | 822 | system("git-read-tree", $branch); |
46541669 | 823 | die "read-tree failed: $?\n" if $?; |
a57a9493 | 824 | } |
46e63efc | 825 | $last_branch = $branch if $branch ne $last_branch; |
a57a9493 MU |
826 | $state = 9; |
827 | } elsif($state == 8) { | |
828 | $logmsg .= "$_\n"; | |
8b8840e0 | 829 | } elsif($state == 9 and /^\s+(.+?):(INITIAL|\d+(?:\.\d+)+)->(\d+(?:\.\d+)+)\s*$/) { |
a57a9493 | 830 | # VERSION:1.96->1.96.2.1 |
2a3e1a85 | 831 | my $init = ($2 eq "INITIAL"); |
a57a9493 | 832 | my $fn = $1; |
f65ae603 MU |
833 | my $rev = $3; |
834 | $fn =~ s#^/+##; | |
2eb6d82e | 835 | my ($tmpname, $size) = $cvs->file($fn,$rev); |
8b8840e0 MU |
836 | if($size == -1) { |
837 | push(@old,$fn); | |
838 | print "Drop $fn\n" if $opt_v; | |
839 | } else { | |
840 | print "".($init ? "New" : "Update")." $fn: $size bytes\n" if $opt_v; | |
841 | open my $F, '-|', "git-hash-object -w $tmpname" | |
842 | or die "Cannot create object: $!\n"; | |
843 | my $sha = <$F>; | |
844 | chomp $sha; | |
845 | close $F; | |
846 | my $mode = pmode($cvs->{'mode'}); | |
847 | push(@new,[$mode, $sha, $fn]); # may be resurrected! | |
848 | } | |
2eb6d82e | 849 | unlink($tmpname); |
b0921331 | 850 | } elsif($state == 9 and /^\s+(.+?):\d+(?:\.\d+)+->(\d+(?:\.\d+)+)\(DEAD\)\s*$/) { |
f65ae603 MU |
851 | my $fn = $1; |
852 | $fn =~ s#^/+##; | |
853 | push(@old,$fn); | |
8b8840e0 | 854 | print "Delete $fn\n" if $opt_v; |
a57a9493 MU |
855 | } elsif($state == 9 and /^\s*$/) { |
856 | $state = 10; | |
857 | } elsif(($state == 9 or $state == 10) and /^-+$/) { | |
858 | &$commit(); | |
859 | $state = 1; | |
860 | } elsif($state == 11 and /^-+$/) { | |
861 | $state = 1; | |
862 | } elsif(/^-+$/) { # end of unknown-line processing | |
863 | $state = 1; | |
864 | } elsif($state != 11) { # ignore stuff when skipping | |
865 | print "* UNKNOWN LINE * $_\n"; | |
866 | } | |
867 | } | |
868 | &$commit() if $branch and $state != 11; | |
d4f8b390 | 869 | |
79ee456c SV |
870 | unlink($git_index); |
871 | ||
210569f9 SV |
872 | if (defined $orig_git_index) { |
873 | $ENV{GIT_INDEX_FILE} = $orig_git_index; | |
874 | } else { | |
875 | delete $ENV{GIT_INDEX_FILE}; | |
876 | } | |
877 | ||
46541669 MU |
878 | # Now switch back to the branch we were in before all of this happened |
879 | if($orig_branch) { | |
79ee456c | 880 | print "DONE\n" if $opt_v; |
210569f9 SV |
881 | system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master") |
882 | if $forward_master; | |
c1c774e7 SV |
883 | unless ($opt_i) { |
884 | system('git-read-tree', '-m', '-u', 'CVS2GIT_HEAD', 'HEAD'); | |
885 | die "read-tree failed: $?\n" if $?; | |
886 | } | |
46541669 MU |
887 | } else { |
888 | $orig_branch = "master"; | |
889 | print "DONE; creating $orig_branch branch\n" if $opt_v; | |
890 | system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master") | |
891 | unless -f "$git_dir/refs/heads/master"; | |
8366a10a | 892 | system('git-update-ref', 'HEAD', "$orig_branch"); |
c1c774e7 SV |
893 | unless ($opt_i) { |
894 | system('git checkout'); | |
895 | die "checkout failed: $?\n" if $?; | |
896 | } | |
46541669 | 897 | } |
210569f9 | 898 | unlink("$git_dir/CVS2GIT_HEAD"); |