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