git-fetch: exit with non-zero status when fast-forward check fails
[gitweb.git] / git-cvsimport.perl
index 50f5d9642a17eef42d9a28e36201a808e655f54f..4310dea1320efa47eee2861b4f886197ca713a2e 100755 (executable)
@@ -161,8 +161,22 @@ sub new {
 sub conn {
        my $self = shift;
        my $repo = $self->{'fullrep'};
-       if($repo =~ s/^:pserver:(?:(.*?)(?::(.*?))?@)?([^:\/]*)(?::(\d*))?//) {
-               my($user,$pass,$serv,$port) = ($1,$2,$3,$4);
+       if($repo =~ s/^:pserver(?:([^:]*)):(?:(.*?)(?::(.*?))?@)?([^:\/]*)(?::(\d*))?//) {
+               my($param,$user,$pass,$serv,$port) = ($1,$2,$3,$4,$5);
+
+               my($proxyhost,$proxyport);
+               if($param && ($param =~ m/proxy=([^;]+)/)) {
+                       $proxyhost = $1;
+                       # Default proxyport, if not specified, is 8080.
+                       $proxyport = 8080;
+                       if($ENV{"CVS_PROXY_PORT"}) {
+                               $proxyport = $ENV{"CVS_PROXY_PORT"};
+                       }
+                       if($param =~ m/proxyport=([^;]+)/){
+                               $proxyport = $1;
+                       }
+               }
+
                $user="anonymous" unless defined $user;
                my $rr2 = "-";
                unless($port) {
@@ -187,13 +201,43 @@ sub conn {
                }
                $pass="A" unless $pass;
 
-               my $s = IO::Socket::INET->new(PeerHost => $serv, PeerPort => $port);
-               die "Socket to $serv: $!\n" unless defined $s;
+               my ($s, $rep);
+               if($proxyhost) {
+
+                       # Use a HTTP Proxy. Only works for HTTP proxies that
+                       # don't require user authentication
+                       #
+                       # See: http://www.ietf.org/rfc/rfc2817.txt
+
+                       $s = IO::Socket::INET->new(PeerHost => $proxyhost, PeerPort => $proxyport);
+                       die "Socket to $proxyhost: $!\n" unless defined $s;
+                       $s->write("CONNECT $serv:$port HTTP/1.1\r\nHost: $serv:$port\r\n\r\n")
+                               or die "Write to $proxyhost: $!\n";
+                       $s->flush();
+
+                       $rep = <$s>;
+
+                       # The answer should look like 'HTTP/1.x 2yy ....'
+                       if(!($rep =~ m#^HTTP/1\.. 2[0-9][0-9]#)) {
+                               die "Proxy connect: $rep\n";
+                       }
+                       # Skip up to the empty line of the proxy server output
+                       # including the response headers.
+                       while ($rep = <$s>) {
+                               last if (!defined $rep ||
+                                        $rep eq "\n" ||
+                                        $rep eq "\r\n");
+                       }
+               } else {
+                       $s = IO::Socket::INET->new(PeerHost => $serv, PeerPort => $port);
+                       die "Socket to $serv: $!\n" unless defined $s;
+               }
+
                $s->write("BEGIN AUTH REQUEST\n$repo\n$user\n$pass\nEND AUTH REQUEST\n")
                        or die "Write to $serv: $!\n";
                $s->flush();
 
-               my $rep = <$s>;
+               $rep = <$s>;
 
                if($rep ne "I LOVE YOU\n") {
                        $rep="<unknown>" unless $rep;
@@ -467,11 +511,6 @@ ($$)
 $orig_git_index = $ENV{GIT_INDEX_FILE} if exists $ENV{GIT_INDEX_FILE};
 
 my %index; # holds filenames of one index per branch
-$index{$opt_o} = tmpnam();
-
-$ENV{GIT_INDEX_FILE} = $index{$opt_o};
-system("git-read-tree", $opt_o);
-die "read-tree failed: $?\n" if $?;
 
 unless(-d $git_dir) {
        system("git-init-db");
@@ -499,31 +538,18 @@ ($$)
        $orig_branch = $last_branch;
        $tip_at_start = `git-rev-parse --verify HEAD`;
 
-       # populate index
-       unless ($index{$last_branch}) {
-           $index{$last_branch} = tmpnam();
-       }
-       $ENV{GIT_INDEX_FILE} = $index{$last_branch};
-       system('git-read-tree', $last_branch);
-       die "read-tree failed: $?\n" if $?;
-
        # Get the last import timestamps
-       opendir(D,"$git_dir/refs/heads");
-       while(defined(my $head = readdir(D))) {
-               next if $head =~ /^\./;
-               open(F,"$git_dir/refs/heads/$head")
-                       or die "Bad head branch: $head: $!\n";
-               chomp(my $ftag = <F>);
-               close(F);
-               open(F,"git-cat-file commit $ftag |");
-               while(<F>) {
-                       next unless /^author\s.*\s(\d+)\s[-+]\d{4}$/;
-                       $branch_date{$head} = $1;
-                       last;
-               }
-               close(F);
+       my $fmt = '($ref, $author) = (%(refname), %(author));';
+       open(H, "git-for-each-ref --perl --format='$fmt' refs/heads |") or
+               die "Cannot run git-for-each-ref: $!\n";
+       while(defined(my $entry = <H>)) {
+               my ($ref, $author);
+               eval($entry) || die "cannot eval refs list: $@";
+               my ($head) = ($ref =~ m|^refs/heads/(.*)|);
+               $author =~ /^.*\s(\d+)\s[-+]\d{4}$/;
+               $branch_date{$head} = $1;
        }
-       closedir(D);
+       close(H);
 }
 
 -d $git_dir
@@ -623,6 +649,27 @@ ()
 $ignorebranch{'#CVSPS_NO_BRANCH'} = 1;
 
 sub commit {
+       if ($branch eq $opt_o && !$index{branch} && !get_headref($branch, $git_dir)) {
+           # looks like an initial commit
+           # use the index primed by git-init-db
+           $ENV{GIT_INDEX_FILE} = '.git/index';
+           $index{$branch} = '.git/index';
+       } else {
+           # use an index per branch to speed up
+           # imports of projects with many branches
+           unless ($index{$branch}) {
+               $index{$branch} = tmpnam();
+               $ENV{GIT_INDEX_FILE} = $index{$branch};
+               if ($ancestor) {
+                   system("git-read-tree", $ancestor);
+               } else {
+                   system("git-read-tree", $branch);
+               }
+               die "read-tree failed: $?\n" if $?;
+           }
+       }
+        $ENV{GIT_INDEX_FILE} = $index{$branch};
+
        update_index(@old, @new);
        @old = @new = ();
        my $tree = write_tree();
@@ -811,30 +858,6 @@ sub commit {
                        close(H)
                                or die "Could not write branch $branch: $!";
                }
-               if(($ancestor || $branch) ne $last_branch) {
-                       print "Switching from $last_branch to $branch\n" if $opt_v;
-                       unless ($index{$branch}) {
-                           $index{$branch} = tmpnam();
-                           $ENV{GIT_INDEX_FILE} = $index{$branch};
-                           system("git-read-tree", $branch);
-                           die "read-tree failed: $?\n" if $?;
-                       }
-                       # just in case
-                       $ENV{GIT_INDEX_FILE} = $index{$branch};
-                       if ($ancestor) {
-                           print "have ancestor $ancestor" if $opt_v;
-                           system("git-read-tree", $ancestor);
-                           die "read-tree failed: $?\n" if $?;
-                       }
-               } else {
-                       # just in case
-                       unless ($index{$branch}) {
-                           $index{$branch} = tmpnam();
-                           $ENV{GIT_INDEX_FILE} = $index{$branch};
-                           system("git-read-tree", $branch);
-                           die "read-tree failed: $?\n" if $?;
-                       }
-               }
                $last_branch = $branch if $branch ne $last_branch;
                $state = 9;
        } elsif($state == 8) {
@@ -897,8 +920,20 @@ sub commit {
 }
 commit() if $branch and $state != 11;
 
+# The heuristic of repacking every 1024 commits can leave a
+# lot of unpacked data.  If there is more than 1MB worth of
+# not-packed objects, repack once more.
+my $line = `git-count-objects`;
+if ($line =~ /^(\d+) objects, (\d+) kilobytes$/) {
+  my ($n_objects, $kb) = ($1, $2);
+  1024 < $kb
+    and system("git repack -a -d");
+}
+
 foreach my $git_index (values %index) {
-    unlink($git_index);
+    if ($git_index ne '.git/index') {
+       unlink($git_index);
+    }
 }
 
 if (defined $orig_git_index) {