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