cvsimport: perform string comparison on "HEAD"
[gitweb.git] / git-cvsimport-script
index d61175613dc4ca522642d9113d7e54a5dfb03cf2..8e0e4ac4b2e4bd2f8da3ad4511e5f9af371f9e2b 100755 (executable)
@@ -61,6 +61,8 @@ package CVSconn;
 # Basic CVS dialog.
 # We're only interested in connecting and downloading, so ...
 
+use POSIX qw(strftime dup2);
+
 sub new {
        my($what,$repo,$subdir) = @_;
        $what=ref($what) if ref($what);
@@ -180,7 +182,7 @@ sub _file {
        $self->{'socketo'}->write("Argument $self->{'subdir'}/$fn\n") or return undef;
        $self->{'socketo'}->write("Directory .\n") or return undef;
        $self->{'socketo'}->write("$self->{'repo'}\n") or return undef;
-       $self->{'socketo'}->write("Sticky T1.1\n") or return undef;
+       # $self->{'socketo'}->write("Sticky T1.0\n") or return undef;
        $self->{'socketo'}->write("co\n") or return undef;
        $self->{'socketo'}->flush() or return undef;
        $self->{'lines'} = 0;
@@ -321,6 +323,7 @@ sub getwd() {
 chdir($git_tree);
 
 my $last_branch = "";
+my $orig_branch = "";
 my %branch_date;
 
 my $git_dir = $ENV{"GIT_DIR"} || ".git";
@@ -333,9 +336,14 @@ unless(-d $git_dir) {
        die "Cannot init an empty tree: $?\n" if $?;
 
        $last_branch = $opt_o;
+       $orig_branch = "";
 } else {
        $last_branch = basename(readlink("$git_dir/HEAD"));
-       die "Cannot read the last branch name: $!\n" unless $last_branch;
+       unless($last_branch) {
+               warn "Cannot read the last branch name: $! -- assuming 'master'\n";
+               $last_branch = "master";
+       }
+       $orig_branch = $last_branch;
 
        # Get the last import timestamps
        opendir(D,"$git_dir/refs/heads");
@@ -389,10 +397,28 @@ my($patchset,$date,$author,$branch,$ancestor,$tag,$logmsg);
 my(@old,@new);
 my $commit = sub {
        my $pid;
-       system("git-update-cache","--force-remove","--",@old) if @old;
-       die "Cannot remove files: $?\n" if $?;
-       system("git-update-cache","--add","--",@new) if @new;
-       die "Cannot add files: $?\n" if $?;
+       while(@old) {
+               my @o2;
+               if(@old > 55) {
+                       @o2 = splice(@old,0,50);
+               } else {
+                       @o2 = @old;
+                       @old = ();
+               }
+               system("git-update-cache","--force-remove","--",@o2);
+               die "Cannot remove files: $?\n" if $?;
+       }
+       while(@new) {
+               my @n2;
+               if(@new > 55) {
+                       @n2 = splice(@new,0,50);
+               } else {
+                       @n2 = @new;
+                       @new = ();
+               }
+               system("git-update-cache","--add","--",@n2);
+               die "Cannot add files: $?\n" if $?;
+       }
 
        $pid = open(C,"-|");
        die "Cannot fork: $!" unless defined $pid;
@@ -472,9 +498,6 @@ my $commit = sub {
                        or die "Cannot write tag $branch: $!\n";
                print "Created tag '$tag' on '$branch'\n" if $opt_v;
        }
-
-       @old = ();
-       @new = ();
 };
 
 while(<CVS>) {
@@ -506,7 +529,7 @@ while(<CVS>) {
        } elsif($state == 5 and s/^Ancestor branch:\s+//) {
                s/\s+$//;
                $ancestor = $_;
-               $ancestor = $opt_o if $ancestor == "HEAD";
+               $ancestor = $opt_o if $ancestor eq "HEAD";
                $state = 6;
        } elsif($state == 5) {
                $ancestor = undef;
@@ -556,8 +579,8 @@ while(<CVS>) {
                }
                if(($ancestor || $branch) ne $last_branch) {
                        print "Switching from $last_branch to $branch\n" if $opt_v;
-                       system("git-read-tree -m -u $last_branch $branch");
-                       die "read-tree $branch failed at $branch: $?\n" if $?;
+                       system("git-read-tree","-m","-u","$last_branch","$branch");
+                       die "read-tree failed: $?\n" if $?;
                }
                if($branch ne $last_branch) {
                        unlink("$git_dir/HEAD");
@@ -571,7 +594,9 @@ while(<CVS>) {
 #      VERSION:1.96->1.96.2.1
                my $init = ($2 eq "INITIAL");
                my $fn = $1;
-               my $data = $cvs->file($fn,$3);
+               my $rev = $3;
+               $fn =~ s#^/+##;
+               my $data = $cvs->file($fn,$rev);
                print "".($init ? "New" : "Update")." $fn: ".length($data)." bytes.\n";
                mkpath(dirname($fn),$opt_v);
                open(F,"> ./$fn")
@@ -583,7 +608,9 @@ while(<CVS>) {
                chmod(pmode($cvs->{'mode'}), $fn);
                push(@new,$fn); # may be resurrected!
        } elsif($state == 9 and /^\s+(\S+):\d(?:\.\d+)+->(\d(?:\.\d+)+)\(DEAD\)\s*$/) {
-               push(@old,$1);
+               my $fn = $1;
+               $fn =~ s#^/+##;
+               push(@old,$fn);
        } elsif($state == 9 and /^\s*$/) {
                $state = 10;
        } elsif(($state == 9 or $state == 10) and /^-+$/) {
@@ -599,4 +626,19 @@ while(<CVS>) {
 }
 &$commit() if $branch and $state != 11;
 
-print "DONE\n" if $opt_v;
+# Now switch back to the branch we were in before all of this happened
+if($orig_branch) {
+       print "DONE; switching back to $orig_branch\n" if $opt_v;
+} else {
+       $orig_branch = "master";
+       print "DONE; creating $orig_branch branch\n" if $opt_v;
+       system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
+               unless -f "$git_dir/refs/heads/master";
+}
+
+system("git-read-tree","-m","-u","$last_branch","$orig_branch");
+die "read-tree failed: $?\n" if $?;
+
+unlink("$git_dir/HEAD");
+symlink("refs/heads/$orig_branch","$git_dir/HEAD");
+