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