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