]>
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); | |
a57a9493 MU |
27 | |
28 | $SIG{'PIPE'}="IGNORE"; | |
29 | $ENV{'TZ'}="UTC"; | |
30 | ||
28537171 | 31 | our($opt_h,$opt_o,$opt_v,$opt_d,$opt_p,$opt_C,$opt_z); |
a57a9493 MU |
32 | |
33 | sub usage() { | |
34 | print STDERR <<END; | |
2a3e1a85 | 35 | Usage: ${\basename $0} # fetch/update GIT from CVS |
28537171 SV |
36 | [ -o branch-for-HEAD ] [ -h ] [ -v ] [ -d CVSROOT ] |
37 | [ -p opts-for-cvsps ] [ -C GIT_repository ] [ -z fuzz ] | |
f9714a4a | 38 | [ CVS_module ] |
a57a9493 MU |
39 | END |
40 | exit(1); | |
41 | } | |
42 | ||
f13bbe7f | 43 | getopts("hvo:d:p:C:z:") or usage(); |
a57a9493 MU |
44 | usage if $opt_h; |
45 | ||
f9714a4a | 46 | @ARGV <= 1 or usage(); |
a57a9493 | 47 | |
2a3e1a85 MU |
48 | if($opt_d) { |
49 | $ENV{"CVSROOT"} = $opt_d; | |
f9714a4a SV |
50 | } elsif(-f 'CVS/Root') { |
51 | open my $f, '<', 'CVS/Root' or die 'Failed to open CVS/Root'; | |
52 | $opt_d = <$f>; | |
53 | chomp $opt_d; | |
54 | close $f; | |
55 | $ENV{"CVSROOT"} = $opt_d; | |
2a3e1a85 MU |
56 | } elsif($ENV{"CVSROOT"}) { |
57 | $opt_d = $ENV{"CVSROOT"}; | |
58 | } else { | |
59 | die "CVSROOT needs to be set"; | |
60 | } | |
61 | $opt_o ||= "origin"; | |
f9714a4a | 62 | my $git_tree = $opt_C; |
2a3e1a85 MU |
63 | $git_tree ||= "."; |
64 | ||
f9714a4a SV |
65 | my $cvs_tree; |
66 | if ($#ARGV == 0) { | |
67 | $cvs_tree = $ARGV[0]; | |
68 | } elsif (-f 'CVS/Repository') { | |
69 | open my $f, '<', 'CVS/Repository' or | |
70 | die 'Failed to open CVS/Repository'; | |
71 | $cvs_tree = <$f>; | |
72 | chomp $cvs_tree; | |
73 | close $f | |
74 | } else { | |
75 | usage(); | |
76 | } | |
77 | ||
a57a9493 MU |
78 | select(STDERR); $|=1; select(STDOUT); |
79 | ||
80 | ||
81 | package CVSconn; | |
82 | # Basic CVS dialog. | |
2a3e1a85 | 83 | # We're only interested in connecting and downloading, so ... |
a57a9493 | 84 | |
2eb6d82e SV |
85 | use File::Spec; |
86 | use File::Temp qw(tempfile); | |
f65ae603 MU |
87 | use POSIX qw(strftime dup2); |
88 | ||
a57a9493 MU |
89 | sub new { |
90 | my($what,$repo,$subdir) = @_; | |
91 | $what=ref($what) if ref($what); | |
92 | ||
93 | my $self = {}; | |
94 | $self->{'buffer'} = ""; | |
95 | bless($self,$what); | |
96 | ||
97 | $repo =~ s#/+$##; | |
98 | $self->{'fullrep'} = $repo; | |
99 | $self->conn(); | |
100 | ||
101 | $self->{'subdir'} = $subdir; | |
102 | $self->{'lines'} = undef; | |
103 | ||
104 | return $self; | |
105 | } | |
106 | ||
107 | sub conn { | |
108 | my $self = shift; | |
109 | my $repo = $self->{'fullrep'}; | |
110 | if($repo =~ s/^:pserver:(?:(.*?)(?::(.*?))?@)?([^:\/]*)(?::(\d*))?//) { | |
111 | my($user,$pass,$serv,$port) = ($1,$2,$3,$4); | |
112 | $user="anonymous" unless defined $user; | |
2a3e1a85 | 113 | my $rr2 = "-"; |
a57a9493 MU |
114 | unless($port) { |
115 | $rr2 = ":pserver:$user\@$serv:$repo"; | |
116 | $port=2401; | |
117 | } | |
118 | my $rr = ":pserver:$user\@$serv:$port$repo"; | |
119 | ||
120 | unless($pass) { | |
121 | open(H,$ENV{'HOME'}."/.cvspass") and do { | |
122 | # :pserver:cvs@mea.tmt.tele.fi:/cvsroot/zmailer Ah<Z | |
123 | while(<H>) { | |
124 | chomp; | |
125 | s/^\/\d+\s+//; | |
126 | my ($w,$p) = split(/\s/,$_,2); | |
127 | if($w eq $rr or $w eq $rr2) { | |
128 | $pass = $p; | |
129 | last; | |
130 | } | |
131 | } | |
132 | }; | |
133 | } | |
134 | $pass="A" unless $pass; | |
135 | ||
a57a9493 MU |
136 | my $s = IO::Socket::INET->new(PeerHost => $serv, PeerPort => $port); |
137 | die "Socket to $serv: $!\n" unless defined $s; | |
138 | $s->write("BEGIN AUTH REQUEST\n$repo\n$user\n$pass\nEND AUTH REQUEST\n") | |
139 | or die "Write to $serv: $!\n"; | |
140 | $s->flush(); | |
141 | ||
142 | my $rep = <$s>; | |
143 | ||
144 | if($rep ne "I LOVE YOU\n") { | |
145 | $rep="<unknown>" unless $rep; | |
146 | die "AuthReply: $rep\n"; | |
147 | } | |
148 | $self->{'socketo'} = $s; | |
149 | $self->{'socketi'} = $s; | |
34155390 | 150 | } else { # local or ext: Fork off our own cvs server. |
a57a9493 MU |
151 | my $pr = IO::Pipe->new(); |
152 | my $pw = IO::Pipe->new(); | |
153 | my $pid = fork(); | |
154 | die "Fork: $!\n" unless defined $pid; | |
8d0ea311 SV |
155 | my $cvs = 'cvs'; |
156 | $cvs = $ENV{CVS_SERVER} if exists $ENV{CVS_SERVER}; | |
34155390 SV |
157 | my $rsh = 'rsh'; |
158 | $rsh = $ENV{CVS_RSH} if exists $ENV{CVS_RSH}; | |
159 | ||
160 | my @cvs = ($cvs, 'server'); | |
161 | my ($local, $user, $host); | |
162 | $local = $repo =~ s/:local://; | |
163 | if (!$local) { | |
164 | $repo =~ s/:ext://; | |
165 | $local = !($repo =~ s/^(?:([^\@:]+)\@)?([^:]+)://); | |
166 | ($user, $host) = ($1, $2); | |
167 | } | |
168 | if (!$local) { | |
169 | if ($user) { | |
170 | unshift @cvs, $rsh, '-l', $user, $host; | |
171 | } else { | |
172 | unshift @cvs, $rsh, $host; | |
173 | } | |
174 | } | |
175 | ||
a57a9493 MU |
176 | unless($pid) { |
177 | $pr->writer(); | |
178 | $pw->reader(); | |
a57a9493 MU |
179 | dup2($pw->fileno(),0); |
180 | dup2($pr->fileno(),1); | |
181 | $pr->close(); | |
182 | $pw->close(); | |
34155390 | 183 | exec(@cvs); |
a57a9493 MU |
184 | } |
185 | $pw->writer(); | |
186 | $pr->reader(); | |
187 | $self->{'socketo'} = $pw; | |
188 | $self->{'socketi'} = $pr; | |
189 | } | |
190 | $self->{'socketo'}->write("Root $repo\n"); | |
191 | ||
192 | # Trial and error says that this probably is the minimum set | |
193 | $self->{'socketo'}->write("Valid-responses ok error Valid-requests Mode M Mbinary E F Checked-in Created Updated Merged Removed\n"); | |
194 | ||
195 | $self->{'socketo'}->write("valid-requests\n"); | |
196 | $self->{'socketo'}->flush(); | |
197 | ||
198 | chomp(my $rep=$self->readline()); | |
199 | if($rep !~ s/^Valid-requests\s*//) { | |
200 | $rep="<unknown>" unless $rep; | |
201 | die "Expected Valid-requests from server, but got: $rep\n"; | |
202 | } | |
203 | chomp(my $res=$self->readline()); | |
204 | die "validReply: $res\n" if $res ne "ok"; | |
205 | ||
206 | $self->{'socketo'}->write("UseUnchanged\n") if $rep =~ /\bUseUnchanged\b/; | |
207 | $self->{'repo'} = $repo; | |
208 | } | |
209 | ||
210 | sub readline { | |
211 | my($self) = @_; | |
212 | return $self->{'socketi'}->getline(); | |
213 | } | |
214 | ||
215 | sub _file { | |
216 | # Request a file with a given revision. | |
217 | # Trial and error says this is a good way to do it. :-/ | |
218 | my($self,$fn,$rev) = @_; | |
219 | $self->{'socketo'}->write("Argument -N\n") or return undef; | |
220 | $self->{'socketo'}->write("Argument -P\n") or return undef; | |
221 | # $self->{'socketo'}->write("Argument -ko\n") or return undef; | |
222 | # -ko: Linus' version doesn't use it | |
223 | $self->{'socketo'}->write("Argument -r\n") or return undef; | |
224 | $self->{'socketo'}->write("Argument $rev\n") or return undef; | |
225 | $self->{'socketo'}->write("Argument --\n") or return undef; | |
226 | $self->{'socketo'}->write("Argument $self->{'subdir'}/$fn\n") or return undef; | |
227 | $self->{'socketo'}->write("Directory .\n") or return undef; | |
228 | $self->{'socketo'}->write("$self->{'repo'}\n") or return undef; | |
4f7c0caa | 229 | # $self->{'socketo'}->write("Sticky T1.0\n") or return undef; |
a57a9493 MU |
230 | $self->{'socketo'}->write("co\n") or return undef; |
231 | $self->{'socketo'}->flush() or return undef; | |
232 | $self->{'lines'} = 0; | |
233 | return 1; | |
234 | } | |
235 | sub _line { | |
236 | # Read a line from the server. | |
237 | # ... except that 'line' may be an entire file. ;-) | |
2eb6d82e | 238 | my($self, $fh) = @_; |
a57a9493 MU |
239 | die "Not in lines" unless defined $self->{'lines'}; |
240 | ||
241 | my $line; | |
2eb6d82e | 242 | my $res=0; |
a57a9493 MU |
243 | while(defined($line = $self->readline())) { |
244 | # M U gnupg-cvs-rep/AUTHORS | |
245 | # Updated gnupg-cvs-rep/ | |
246 | # /daten/src/rsync/gnupg-cvs-rep/AUTHORS | |
247 | # /AUTHORS/1.1///T1.1 | |
248 | # u=rw,g=rw,o=rw | |
249 | # 0 | |
250 | # ok | |
251 | ||
252 | if($line =~ s/^(?:Created|Updated) //) { | |
253 | $line = $self->readline(); # path | |
254 | $line = $self->readline(); # Entries line | |
255 | my $mode = $self->readline(); chomp $mode; | |
256 | $self->{'mode'} = $mode; | |
257 | defined (my $cnt = $self->readline()) | |
258 | or die "EOF from server after 'Changed'\n"; | |
259 | chomp $cnt; | |
260 | die "Duh: Filesize $cnt" if $cnt !~ /^\d+$/; | |
261 | $line=""; | |
2eb6d82e | 262 | $res=0; |
a57a9493 MU |
263 | while($cnt) { |
264 | my $buf; | |
265 | my $num = $self->{'socketi'}->read($buf,$cnt); | |
266 | die "Server: Filesize $cnt: $num: $!\n" if not defined $num or $num<=0; | |
2eb6d82e SV |
267 | print $fh $buf; |
268 | $res += $num; | |
a57a9493 MU |
269 | $cnt -= $num; |
270 | } | |
271 | } elsif($line =~ s/^ //) { | |
2eb6d82e SV |
272 | print $fh $line; |
273 | $res += length($line); | |
a57a9493 MU |
274 | } elsif($line =~ /^M\b/) { |
275 | # output, do nothing | |
276 | } elsif($line =~ /^Mbinary\b/) { | |
277 | my $cnt; | |
278 | die "EOF from server after 'Mbinary'" unless defined ($cnt = $self->readline()); | |
279 | chomp $cnt; | |
280 | die "Duh: Mbinary $cnt" if $cnt !~ /^\d+$/ or $cnt<1; | |
281 | $line=""; | |
282 | while($cnt) { | |
283 | my $buf; | |
284 | my $num = $self->{'socketi'}->read($buf,$cnt); | |
285 | die "S: Mbinary $cnt: $num: $!\n" if not defined $num or $num<=0; | |
2eb6d82e SV |
286 | print $fh $buf; |
287 | $res += $num; | |
a57a9493 MU |
288 | $cnt -= $num; |
289 | } | |
290 | } else { | |
291 | chomp $line; | |
292 | if($line eq "ok") { | |
293 | # print STDERR "S: ok (".length($res).")\n"; | |
294 | return $res; | |
295 | } elsif($line =~ s/^E //) { | |
296 | # print STDERR "S: $line\n"; | |
297 | } else { | |
298 | die "Unknown: $line\n"; | |
299 | } | |
300 | } | |
301 | } | |
302 | } | |
303 | sub file { | |
304 | my($self,$fn,$rev) = @_; | |
305 | my $res; | |
306 | ||
2eb6d82e SV |
307 | my ($fh, $name) = tempfile('gitcvs.XXXXXX', |
308 | DIR => File::Spec->tmpdir(), UNLINK => 1); | |
309 | ||
310 | $self->_file($fn,$rev) and $res = $self->_line($fh); | |
311 | ||
312 | if (!defined $res) { | |
313 | # retry | |
314 | $self->conn(); | |
315 | $self->_file($fn,$rev) | |
316 | or die "No file command send\n"; | |
317 | $res = $self->_line($fh); | |
318 | die "No input: $fn $rev\n" unless defined $res; | |
a57a9493 | 319 | } |
c619ad51 | 320 | close ($fh); |
a57a9493 | 321 | |
2eb6d82e | 322 | return ($name, $res); |
a57a9493 MU |
323 | } |
324 | ||
325 | ||
326 | package main; | |
327 | ||
2a3e1a85 | 328 | my $cvs = CVSconn->new($opt_d, $cvs_tree); |
a57a9493 MU |
329 | |
330 | ||
331 | sub pdate($) { | |
332 | my($d) = @_; | |
333 | m#(\d{2,4})/(\d\d)/(\d\d)\s(\d\d):(\d\d)(?::(\d\d))?# | |
334 | or die "Unparseable date: $d\n"; | |
335 | my $y=$1; $y-=1900 if $y>1900; | |
336 | return timegm($6||0,$5,$4,$3,$2-1,$y); | |
9718a00b TM |
337 | } |
338 | ||
a57a9493 MU |
339 | sub pmode($) { |
340 | my($mode) = @_; | |
341 | my $m = 0; | |
342 | my $mm = 0; | |
343 | my $um = 0; | |
344 | for my $x(split(//,$mode)) { | |
345 | if($x eq ",") { | |
346 | $m |= $mm&$um; | |
347 | $mm = 0; | |
348 | $um = 0; | |
349 | } elsif($x eq "u") { $um |= 0700; | |
350 | } elsif($x eq "g") { $um |= 0070; | |
351 | } elsif($x eq "o") { $um |= 0007; | |
352 | } elsif($x eq "r") { $mm |= 0444; | |
353 | } elsif($x eq "w") { $mm |= 0222; | |
354 | } elsif($x eq "x") { $mm |= 0111; | |
355 | } elsif($x eq "=") { # do nothing | |
356 | } else { die "Unknown mode: $mode\n"; | |
357 | } | |
358 | } | |
359 | $m |= $mm&$um; | |
360 | return $m; | |
361 | } | |
d4f8b390 | 362 | |
a57a9493 MU |
363 | sub getwd() { |
364 | my $pwd = `pwd`; | |
365 | chomp $pwd; | |
366 | return $pwd; | |
d4f8b390 LT |
367 | } |
368 | ||
a57a9493 MU |
369 | -d $git_tree |
370 | or mkdir($git_tree,0777) | |
371 | or die "Could not create $git_tree: $!"; | |
372 | chdir($git_tree); | |
d4f8b390 | 373 | |
a57a9493 | 374 | my $last_branch = ""; |
46541669 | 375 | my $orig_branch = ""; |
210569f9 | 376 | my $forward_master = 0; |
a57a9493 MU |
377 | my %branch_date; |
378 | ||
379 | my $git_dir = $ENV{"GIT_DIR"} || ".git"; | |
380 | $git_dir = getwd()."/".$git_dir unless $git_dir =~ m#^/#; | |
381 | $ENV{"GIT_DIR"} = $git_dir; | |
79ee456c SV |
382 | my $orig_git_index; |
383 | $orig_git_index = $ENV{GIT_INDEX_FILE} if exists $ENV{GIT_INDEX_FILE}; | |
384 | my ($git_ih, $git_index) = tempfile('gitXXXXXX', SUFFIX => '.idx', | |
385 | DIR => File::Spec->tmpdir()); | |
386 | close ($git_ih); | |
387 | $ENV{GIT_INDEX_FILE} = $git_index; | |
a57a9493 MU |
388 | unless(-d $git_dir) { |
389 | system("git-init-db"); | |
390 | die "Cannot init the GIT db at $git_tree: $?\n" if $?; | |
391 | system("git-read-tree"); | |
392 | die "Cannot init an empty tree: $?\n" if $?; | |
393 | ||
394 | $last_branch = $opt_o; | |
46541669 | 395 | $orig_branch = ""; |
a57a9493 | 396 | } else { |
866d1310 | 397 | -f "$git_dir/refs/heads/$opt_o" |
4c24e089 MU |
398 | or die "Branch '$opt_o' does not exist.\n". |
399 | "Either use the correct '-o branch' option,\n". | |
400 | "or import to a new repository.\n"; | |
401 | ||
a57a9493 | 402 | $last_branch = basename(readlink("$git_dir/HEAD")); |
46541669 MU |
403 | unless($last_branch) { |
404 | warn "Cannot read the last branch name: $! -- assuming 'master'\n"; | |
405 | $last_branch = "master"; | |
406 | } | |
407 | $orig_branch = $last_branch; | |
210569f9 SV |
408 | if (-f "$git_dir/CVS2GIT_HEAD") { |
409 | die <<EOM; | |
410 | CVS2GIT_HEAD exists. | |
411 | Make sure your working directory corresponds to HEAD and remove CVS2GIT_HEAD. | |
412 | You may need to run | |
413 | ||
414 | git-read-tree -m -u CVS2GIT_HEAD HEAD | |
415 | EOM | |
416 | } | |
417 | system('cp', "$git_dir/HEAD", "$git_dir/CVS2GIT_HEAD"); | |
418 | ||
419 | $forward_master = | |
420 | $opt_o ne 'master' && -f "$git_dir/refs/heads/master" && | |
421 | system('cmp', '-s', "$git_dir/refs/heads/master", | |
422 | "$git_dir/refs/heads/$opt_o") == 0; | |
a57a9493 | 423 | |
79ee456c SV |
424 | # populate index |
425 | system('git-read-tree', $last_branch); | |
17501132 | 426 | die "read-tree failed: $?\n" if $?; |
a57a9493 MU |
427 | |
428 | # Get the last import timestamps | |
429 | opendir(D,"$git_dir/refs/heads"); | |
430 | while(defined(my $head = readdir(D))) { | |
431 | next if $head =~ /^\./; | |
432 | open(F,"$git_dir/refs/heads/$head") | |
433 | or die "Bad head branch: $head: $!\n"; | |
434 | chomp(my $ftag = <F>); | |
435 | close(F); | |
436 | open(F,"git-cat-file commit $ftag |"); | |
437 | while(<F>) { | |
438 | next unless /^author\s.*\s(\d+)\s[-+]\d{4}$/; | |
439 | $branch_date{$head} = $1; | |
440 | last; | |
441 | } | |
442 | close(F); | |
443 | } | |
444 | closedir(D); | |
445 | } | |
446 | ||
447 | -d $git_dir | |
448 | or die "Could not create git subdir ($git_dir).\n"; | |
449 | ||
450 | my $pid = open(CVS,"-|"); | |
451 | die "Cannot fork: $!\n" unless defined $pid; | |
452 | unless($pid) { | |
2be4fcc3 MU |
453 | my @opt; |
454 | @opt = split(/,/,$opt_p) if defined $opt_p; | |
28537171 | 455 | unshift @opt, '-z', $opt_z if defined $opt_z; |
6e7e37b0 | 456 | exec("cvsps",@opt,"-u","-A","--cvs-direct",'--root',$opt_d,$cvs_tree); |
a57a9493 MU |
457 | die "Could not start cvsps: $!\n"; |
458 | } | |
459 | ||
460 | ||
461 | ## cvsps output: | |
462 | #--------------------- | |
463 | #PatchSet 314 | |
464 | #Date: 1999/09/18 13:03:59 | |
465 | #Author: wkoch | |
466 | #Branch: STABLE-BRANCH-1-0 | |
467 | #Ancestor branch: HEAD | |
468 | #Tag: (none) | |
469 | #Log: | |
470 | # See ChangeLog: Sat Sep 18 13:03:28 CEST 1999 Werner Koch | |
471 | #Members: | |
472 | # README:1.57->1.57.2.1 | |
473 | # VERSION:1.96->1.96.2.1 | |
474 | # | |
475 | #--------------------- | |
476 | ||
477 | my $state = 0; | |
478 | ||
479 | my($patchset,$date,$author,$branch,$ancestor,$tag,$logmsg); | |
480 | my(@old,@new); | |
481 | my $commit = sub { | |
482 | my $pid; | |
4abdecbf MU |
483 | while(@old) { |
484 | my @o2; | |
485 | if(@old > 55) { | |
486 | @o2 = splice(@old,0,50); | |
487 | } else { | |
488 | @o2 = @old; | |
489 | @old = (); | |
490 | } | |
491 | system("git-update-cache","--force-remove","--",@o2); | |
492 | die "Cannot remove files: $?\n" if $?; | |
493 | } | |
494 | while(@new) { | |
495 | my @n2; | |
2eb6d82e SV |
496 | if(@new > 12) { |
497 | @n2 = splice(@new,0,10); | |
4abdecbf MU |
498 | } else { |
499 | @n2 = @new; | |
500 | @new = (); | |
501 | } | |
2eb6d82e SV |
502 | system("git-update-cache","--add", |
503 | (map { ('--cacheinfo', @$_) } @n2)); | |
4abdecbf MU |
504 | die "Cannot add files: $?\n" if $?; |
505 | } | |
a57a9493 MU |
506 | |
507 | $pid = open(C,"-|"); | |
508 | die "Cannot fork: $!" unless defined $pid; | |
509 | unless($pid) { | |
510 | exec("git-write-tree"); | |
511 | die "Cannot exec git-write-tree: $!\n"; | |
512 | } | |
513 | chomp(my $tree = <C>); | |
514 | length($tree) == 40 | |
515 | or die "Cannot get tree id ($tree): $!\n"; | |
516 | close(C) | |
517 | or die "Error running git-write-tree: $?\n"; | |
518 | print "Tree ID $tree\n" if $opt_v; | |
519 | ||
520 | my $parent = ""; | |
521 | if(open(C,"$git_dir/refs/heads/$last_branch")) { | |
522 | chomp($parent = <C>); | |
523 | close(C); | |
524 | length($parent) == 40 | |
525 | or die "Cannot get parent id ($parent): $!\n"; | |
526 | print "Parent ID $parent\n" if $opt_v; | |
527 | } | |
528 | ||
17501132 SV |
529 | my $pr = IO::Pipe->new() or die "Cannot open pipe: $!\n"; |
530 | my $pw = IO::Pipe->new() or die "Cannot open pipe: $!\n"; | |
2a3e1a85 MU |
531 | $pid = fork(); |
532 | die "Fork: $!\n" unless defined $pid; | |
a57a9493 | 533 | unless($pid) { |
2a3e1a85 MU |
534 | $pr->writer(); |
535 | $pw->reader(); | |
536 | dup2($pw->fileno(),0); | |
537 | dup2($pr->fileno(),1); | |
538 | $pr->close(); | |
539 | $pw->close(); | |
540 | ||
a57a9493 MU |
541 | my @par = (); |
542 | @par = ("-p",$parent) if $parent; | |
543 | exec("env", | |
544 | "GIT_AUTHOR_NAME=$author", | |
545 | "GIT_AUTHOR_EMAIL=$author", | |
546 | "GIT_AUTHOR_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)), | |
547 | "GIT_COMMITTER_NAME=$author", | |
548 | "GIT_COMMITTER_EMAIL=$author", | |
549 | "GIT_COMMITTER_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)), | |
550 | "git-commit-tree", $tree,@par); | |
551 | die "Cannot exec git-commit-tree: $!\n"; | |
552 | } | |
2a3e1a85 MU |
553 | $pw->writer(); |
554 | $pr->reader(); | |
e371046b MU |
555 | |
556 | # compatibility with git2cvs | |
557 | substr($logmsg,32767) = "" if length($logmsg) > 32767; | |
558 | $logmsg =~ s/[\s\n]+\z//; | |
559 | ||
560 | print $pw "$logmsg\n" | |
a57a9493 | 561 | or die "Error writing to git-commit-tree: $!\n"; |
2a3e1a85 MU |
562 | $pw->close(); |
563 | ||
a57a9493 | 564 | print "Committed patch $patchset ($branch)\n" if $opt_v; |
2a3e1a85 | 565 | chomp(my $cid = <$pr>); |
a57a9493 MU |
566 | length($cid) == 40 |
567 | or die "Cannot get commit id ($cid): $!\n"; | |
568 | print "Commit ID $cid\n" if $opt_v; | |
2a3e1a85 MU |
569 | $pr->close(); |
570 | ||
571 | waitpid($pid,0); | |
572 | die "Error running git-commit-tree: $?\n" if $?; | |
a57a9493 MU |
573 | |
574 | open(C,">$git_dir/refs/heads/$branch") | |
575 | or die "Cannot open branch $branch for update: $!\n"; | |
576 | print C "$cid\n" | |
577 | or die "Cannot write branch $branch for update: $!\n"; | |
578 | close(C) | |
579 | or die "Cannot write branch $branch for update: $!\n"; | |
580 | ||
581 | if($tag) { | |
582 | open(C,">$git_dir/refs/tags/$tag") | |
583 | or die "Cannot create tag $tag: $!\n"; | |
584 | print C "$cid\n" | |
585 | or die "Cannot write tag $branch: $!\n"; | |
586 | close(C) | |
587 | or die "Cannot write tag $branch: $!\n"; | |
588 | print "Created tag '$tag' on '$branch'\n" if $opt_v; | |
589 | } | |
a57a9493 MU |
590 | }; |
591 | ||
592 | while(<CVS>) { | |
593 | chomp; | |
594 | if($state == 0 and /^-+$/) { | |
595 | $state = 1; | |
596 | } elsif($state == 0) { | |
597 | $state = 1; | |
598 | redo; | |
599 | } elsif(($state==0 or $state==1) and s/^PatchSet\s+//) { | |
600 | $patchset = 0+$_; | |
601 | $state=2; | |
602 | } elsif($state == 2 and s/^Date:\s+//) { | |
603 | $date = pdate($_); | |
604 | unless($date) { | |
605 | print STDERR "Could not parse date: $_\n"; | |
606 | $state=0; | |
607 | next; | |
608 | } | |
609 | $state=3; | |
610 | } elsif($state == 3 and s/^Author:\s+//) { | |
611 | s/\s+$//; | |
612 | $author = $_; | |
613 | $state = 4; | |
614 | } elsif($state == 4 and s/^Branch:\s+//) { | |
615 | s/\s+$//; | |
616 | $branch = $_; | |
617 | $state = 5; | |
618 | } elsif($state == 5 and s/^Ancestor branch:\s+//) { | |
619 | s/\s+$//; | |
620 | $ancestor = $_; | |
0fa2824f | 621 | $ancestor = $opt_o if $ancestor eq "HEAD"; |
a57a9493 MU |
622 | $state = 6; |
623 | } elsif($state == 5) { | |
624 | $ancestor = undef; | |
625 | $state = 6; | |
626 | redo; | |
627 | } elsif($state == 6 and s/^Tag:\s+//) { | |
628 | s/\s+$//; | |
629 | if($_ eq "(none)") { | |
630 | $tag = undef; | |
631 | } else { | |
632 | $tag = $_; | |
633 | } | |
634 | $state = 7; | |
635 | } elsif($state == 7 and /^Log:/) { | |
636 | $logmsg = ""; | |
637 | $state = 8; | |
638 | } elsif($state == 8 and /^Members:/) { | |
639 | $branch = $opt_o if $branch eq "HEAD"; | |
640 | if(defined $branch_date{$branch} and $branch_date{$branch} >= $date) { | |
641 | # skip | |
9da07f34 | 642 | print "skip patchset $patchset: $date before $branch_date{$branch}\n" if $opt_v; |
a57a9493 MU |
643 | $state = 11; |
644 | next; | |
645 | } | |
646 | if($ancestor) { | |
647 | if(-f "$git_dir/refs/heads/$branch") { | |
648 | print STDERR "Branch $branch already exists!\n"; | |
649 | $state=11; | |
650 | next; | |
651 | } | |
652 | unless(open(H,"$git_dir/refs/heads/$ancestor")) { | |
653 | print STDERR "Branch $ancestor does not exist!\n"; | |
654 | $state=11; | |
655 | next; | |
656 | } | |
657 | chomp(my $id = <H>); | |
658 | close(H); | |
659 | unless(open(H,"> $git_dir/refs/heads/$branch")) { | |
660 | print STDERR "Could not create branch $branch: $!\n"; | |
661 | $state=11; | |
662 | next; | |
663 | } | |
664 | print H "$id\n" | |
665 | or die "Could not write branch $branch: $!"; | |
666 | close(H) | |
667 | or die "Could not write branch $branch: $!"; | |
668 | } | |
669 | if(($ancestor || $branch) ne $last_branch) { | |
2a3e1a85 | 670 | print "Switching from $last_branch to $branch\n" if $opt_v; |
46e63efc | 671 | system("git-read-tree", $branch); |
46541669 | 672 | die "read-tree failed: $?\n" if $?; |
a57a9493 | 673 | } |
46e63efc | 674 | $last_branch = $branch if $branch ne $last_branch; |
a57a9493 MU |
675 | $state = 9; |
676 | } elsif($state == 8) { | |
677 | $logmsg .= "$_\n"; | |
2a3e1a85 | 678 | } elsif($state == 9 and /^\s+(\S+):(INITIAL|\d(?:\.\d+)+)->(\d(?:\.\d+)+)\s*$/) { |
a57a9493 | 679 | # VERSION:1.96->1.96.2.1 |
2a3e1a85 | 680 | my $init = ($2 eq "INITIAL"); |
a57a9493 | 681 | my $fn = $1; |
f65ae603 MU |
682 | my $rev = $3; |
683 | $fn =~ s#^/+##; | |
2eb6d82e SV |
684 | my ($tmpname, $size) = $cvs->file($fn,$rev); |
685 | print "".($init ? "New" : "Update")." $fn: $size bytes.\n" if $opt_v; | |
686 | open my $F, '-|', "git-write-blob $tmpname" | |
687 | or die "Cannot create object: $!\n"; | |
688 | my $sha = <$F>; | |
689 | chomp $sha; | |
690 | close $F; | |
691 | unlink($tmpname); | |
692 | my $mode = pmode($cvs->{'mode'}); | |
693 | push(@new,[$mode, $sha, $fn]); # may be resurrected! | |
a57a9493 | 694 | } elsif($state == 9 and /^\s+(\S+):\d(?:\.\d+)+->(\d(?:\.\d+)+)\(DEAD\)\s*$/) { |
f65ae603 MU |
695 | my $fn = $1; |
696 | $fn =~ s#^/+##; | |
697 | push(@old,$fn); | |
a57a9493 MU |
698 | } elsif($state == 9 and /^\s*$/) { |
699 | $state = 10; | |
700 | } elsif(($state == 9 or $state == 10) and /^-+$/) { | |
701 | &$commit(); | |
702 | $state = 1; | |
703 | } elsif($state == 11 and /^-+$/) { | |
704 | $state = 1; | |
705 | } elsif(/^-+$/) { # end of unknown-line processing | |
706 | $state = 1; | |
707 | } elsif($state != 11) { # ignore stuff when skipping | |
708 | print "* UNKNOWN LINE * $_\n"; | |
709 | } | |
710 | } | |
711 | &$commit() if $branch and $state != 11; | |
d4f8b390 | 712 | |
79ee456c SV |
713 | unlink($git_index); |
714 | ||
210569f9 SV |
715 | if (defined $orig_git_index) { |
716 | $ENV{GIT_INDEX_FILE} = $orig_git_index; | |
717 | } else { | |
718 | delete $ENV{GIT_INDEX_FILE}; | |
719 | } | |
720 | ||
46541669 MU |
721 | # Now switch back to the branch we were in before all of this happened |
722 | if($orig_branch) { | |
79ee456c | 723 | print "DONE\n" if $opt_v; |
210569f9 SV |
724 | system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master") |
725 | if $forward_master; | |
726 | system('git-read-tree', '-m', '-u', 'CVS2GIT_HEAD', 'HEAD'); | |
727 | die "read-tree failed: $?\n" if $?; | |
46541669 MU |
728 | } else { |
729 | $orig_branch = "master"; | |
730 | print "DONE; creating $orig_branch branch\n" if $opt_v; | |
731 | system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master") | |
732 | unless -f "$git_dir/refs/heads/master"; | |
79ee456c SV |
733 | unlink("$git_dir/HEAD"); |
734 | symlink("refs/heads/$orig_branch","$git_dir/HEAD"); | |
79ee456c SV |
735 | system('git checkout'); |
736 | die "checkout failed: $?\n" if $?; | |
46541669 | 737 | } |
210569f9 | 738 | unlink("$git_dir/CVS2GIT_HEAD"); |