]> asedeno.scripts.mit.edu Git - git.git/blobdiff - git-svn.perl
Merge branch 'js/maint-daemon' into maint
[git.git] / git-svn.perl
index b45c7e3266176336a4d8c6deeaa051c6fd0e9be6..a4ab05155d915fa8f2d8652b5912a447597e091a 100755 (executable)
@@ -197,8 +197,8 @@ for (my $i = 0; $i < @ARGV; $i++) {
        }
 };
 
-# make sure we're always running
-unless ($cmd =~ /(?:clone|init|multi-init)$/) {
+# make sure we're always running at the top-level working directory
+unless ($cmd && $cmd =~ /(?:clone|init|multi-init)$/) {
        unless (-d $ENV{GIT_DIR}) {
                if ($git_dir_user_set) {
                        die "GIT_DIR=$ENV{GIT_DIR} explicitly set, ",
@@ -396,6 +396,7 @@ sub cmd_set_tree {
        }
        $gs->set_tree($_) foreach @revs;
        print "Done committing ",scalar @revs," revisions to SVN\n";
+       unlink $gs->{index};
 }
 
 sub cmd_dcommit {
@@ -417,7 +418,7 @@ sub cmd_dcommit {
                warn "Attempting to commit more than one change while ",
                     "--no-rebase is enabled.\n",
                     "If these changes depend on each other, re-running ",
-                    "without --no-rebase will be required."
+                    "without --no-rebase may be required."
        }
        while (1) {
                my $d = shift @$linear_refs or last;
@@ -452,6 +453,7 @@ sub cmd_dcommit {
                                                               $parents->{$d};
                        }
                        $_fetch_all ? $gs->fetch_all : $gs->fetch;
+                       $last_rev = $cmt_rev;
                        next if $_no_rebase;
 
                        # we always want to rebase against the current HEAD,
@@ -511,9 +513,9 @@ sub cmd_dcommit {
                                $parents = \%p;
                                $linear_refs = \@l;
                        }
-                       $last_rev = $cmt_rev;
                }
        }
+       unlink $gs->{index};
 }
 
 sub cmd_find_rev {
@@ -1281,8 +1283,11 @@ BEGIN {
        }
 }
 
-my %LOCKFILES;
-END { unlink keys %LOCKFILES if %LOCKFILES }
+my (%LOCKFILES, %INDEX_FILES);
+END {
+       unlink keys %LOCKFILES if %LOCKFILES;
+       unlink keys %INDEX_FILES if %INDEX_FILES;
+}
 
 sub resolve_local_globs {
        my ($url, $fetch, $glob_spec) = @_;
@@ -1403,11 +1408,9 @@ sub read_all_remotes {
 }
 
 sub init_vars {
-       if (defined $_repack) {
-               $_repack = 1000 if ($_repack <= 0);
-               $_repack_nr = $_repack;
-               $_repack_flags ||= '-d';
-       }
+       $_repack = 1000 unless (defined $_repack && $_repack > 0);
+       $_repack_nr = $_repack;
+       $_repack_flags ||= '-d';
 }
 
 sub verify_remotes_sanity {
@@ -1753,10 +1756,16 @@ sub svnsync {
        # see if we have it in our config, first:
        eval {
                my $section = "svn-remote.$self->{repo_id}";
-               $svnsync = {
-                 url => tmp_config('--get', "$section.svnsync-url"),
-                 uuid => tmp_config('--get', "$section.svnsync-uuid"),
-               }
+
+               my $url = tmp_config('--get', "$section.svnsync-url");
+               ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
+                  die "doesn't look right - svn:sync-from-url is '$url'\n";
+
+               my $uuid = tmp_config('--get', "$section.svnsync-uuid");
+               ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}) or
+                  die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
+
+               $svnsync = { url => $url, uuid => $uuid }
        };
        if ($svnsync && $svnsync->{url} && $svnsync->{uuid}) {
                return $self->{svnsync} = $svnsync;
@@ -1767,11 +1776,11 @@ sub svnsync {
        my $rp = $self->ra->rev_proplist(0);
 
        my $url = $rp->{'svn:sync-from-url'} or die $err . "url\n";
-       $url =~ m{^[a-z\+]+://} or
+       ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
                   die "doesn't look right - svn:sync-from-url is '$url'\n";
 
        my $uuid = $rp->{'svn:sync-from-uuid'} or die $err . "uuid\n";
-       $uuid =~ m{^[0-9a-f\-]{30,}$} or
+       ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}) or
                   die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
 
        my $section = "svn-remote.$self->{repo_id}";
@@ -1853,6 +1862,7 @@ sub rel_path {
 sub prop_walk {
        my ($self, $path, $rev, $sub) = @_;
 
+       $path =~ s#^/##;
        my ($dirent, undef, $props) = $self->ra->get_dir($path, $rev);
        $path =~ s#^/*#/#g;
        my $p = $path;
@@ -1904,7 +1914,7 @@ sub last_rev_commit {
                ($self->{last_rev}, $self->{last_commit}) = (undef, undef);
                return (undef, undef);
        }
-       my ($rev, $commit) = $self->rev_map_max;
+       my ($rev, $commit) = $self->rev_map_max(1);
        ($self->{last_rev}, $self->{last_commit}) = ($rev, $commit);
        return ($rev, $commit);
 }
@@ -2049,18 +2059,16 @@ sub full_url {
        $self->{url} . (length $self->{path} ? '/' . $self->{path} : '');
 }
 
-sub do_git_commit {
-       my ($self, $log_entry) = @_;
-       my $lr = $self->last_rev;
-       if (defined $lr && $lr >= $log_entry->{revision}) {
-               die "Last fetched revision of ", $self->refname,
-                   " was r$lr, but we are about to fetch: ",
-                   "r$log_entry->{revision}!\n";
-       }
-       if (my $c = $self->rev_map_get($log_entry->{revision})) {
-               croak "$log_entry->{revision} = $c already exists! ",
-                     "Why are we refetching it?\n";
+
+sub set_commit_header_env {
+       my ($log_entry) = @_;
+       my %env;
+       foreach my $ned (qw/NAME EMAIL DATE/) {
+               foreach my $ac (qw/AUTHOR COMMITTER/) {
+                       $env{"GIT_${ac}_${ned}"} = $ENV{"GIT_${ac}_${ned}"};
+               }
        }
+
        $ENV{GIT_AUTHOR_NAME} = $log_entry->{name};
        $ENV{GIT_AUTHOR_EMAIL} = $log_entry->{email};
        $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_entry->{date};
@@ -2071,7 +2079,36 @@ sub do_git_commit {
        $ENV{GIT_COMMITTER_EMAIL} = (defined $log_entry->{commit_email})
                                                ? $log_entry->{commit_email}
                                                : $log_entry->{email};
+       \%env;
+}
+
+sub restore_commit_header_env {
+       my ($env) = @_;
+       foreach my $ned (qw/NAME EMAIL DATE/) {
+               foreach my $ac (qw/AUTHOR COMMITTER/) {
+                       my $k = "GIT_${ac}_${ned}";
+                       if (defined $env->{$k}) {
+                               $ENV{$k} = $env->{$k};
+                       } else {
+                               delete $ENV{$k};
+                       }
+               }
+       }
+}
 
+sub do_git_commit {
+       my ($self, $log_entry) = @_;
+       my $lr = $self->last_rev;
+       if (defined $lr && $lr >= $log_entry->{revision}) {
+               die "Last fetched revision of ", $self->refname,
+                   " was r$lr, but we are about to fetch: ",
+                   "r$log_entry->{revision}!\n";
+       }
+       if (my $c = $self->rev_map_get($log_entry->{revision})) {
+               croak "$log_entry->{revision} = $c already exists! ",
+                     "Why are we refetching it?\n";
+       }
+       my $old_env = set_commit_header_env($log_entry);
        my $tree = $log_entry->{tree};
        if (!defined $tree) {
                $tree = $self->tmp_index_do(sub {
@@ -2086,6 +2123,7 @@ sub do_git_commit {
        defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
                                                                   or croak $!;
        print $msg_fh $log_entry->{log} or croak $!;
+       restore_commit_header_env($old_env);
        unless ($self->no_metadata) {
                print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n"
                              or croak $!;
@@ -2111,7 +2149,7 @@ sub do_git_commit {
                                   0, $self->svm_uuid);
        }
        print " = $commit ($self->{ref_id})\n";
-       if (defined $_repack && (--$_repack_nr == 0)) {
+       if ($_repack && (--$_repack_nr == 0)) {
                $_repack_nr = $_repack;
                # repack doesn't use any arguments with spaces in them, does it?
                print "Running git repack $_repack_flags ...\n";
@@ -2363,11 +2401,20 @@ sub make_log_entry {
 
        my ($commit_name, $commit_email) = ($name, $email);
        if ($_use_log_author) {
-               if ($log_entry{log} =~ /From:\s+(.*?)\s+<(.*)>\s*\n/) {
-                       ($name, $email) = ($1, $2);
-               } elsif ($log_entry{log} =~
-                                     /Signed-off-by:\s+(.*?)\s+<(.*)>\s*\n/) {
+               my $name_field;
+               if ($log_entry{log} =~ /From:\s+(.*\S)\s*\n/i) {
+                       $name_field = $1;
+               } elsif ($log_entry{log} =~ /Signed-off-by:\s+(.*\S)\s*\n/i) {
+                       $name_field = $1;
+               }
+               if (!defined $name_field) {
+                       #
+               } elsif ($name_field =~ /(.*?)\s+<(.*)>/) {
                        ($name, $email) = ($1, $2);
+               } elsif ($name_field =~ /(.*)@/) {
+                       ($name, $email) = ($1, $name_field);
+               } else {
+                       ($name, $email) = ($name_field, 'unknown');
                }
        }
        if (defined $headrev && $self->use_svm_props) {
@@ -2534,11 +2581,16 @@ sub rebuild {
 #     * 4 bytes for the integer representing an SVN revision number
 #     * 20 bytes representing the sha1 of a git commit
 #   - No empty padding records like the old format
+#     (except the last record, which can be overwritten)
 #   - new records are written append-only since SVN revision numbers
 #     increase monotonically
 #   - lookups on SVN revision number are done via a binary search
-#   - Piping the file to xxd(1) -c24 is a good way of dumping it for
-#     viewing or editing, should the need ever arise.
+#   - Piping the file to xxd -c24 is a good way of dumping it for
+#     viewing or editing (piped back through xxd -r), should the need
+#     ever arise.
+#   - The last record can be padding revision with an all-zero sha1
+#     This is used to optimize fetch performance when using multiple
+#     "fetch" directives in .git/config
 #
 # These files are disposable unless noMetadata or useSvmProps is set
 
@@ -2548,16 +2600,32 @@ sub _rev_map_set {
        my $size = (stat($fh))[7];
        ($size % 24) == 0 or croak "inconsistent size: $size";
 
+       my $wr_offset = 0;
        if ($size > 0) {
                sysseek($fh, -24, SEEK_END) or croak "seek: $!";
                my $read = sysread($fh, my $buf, 24) or croak "read: $!";
                $read == 24 or croak "read only $read bytes (!= 24)";
                my ($last_rev, $last_commit) = unpack(rev_map_fmt, $buf);
-               if ($last_rev >= $rev) {
-                       croak "last_rev is higher!: $last_rev >= $rev";
+               if ($last_commit eq ('0' x40)) {
+                       if ($size >= 48) {
+                               sysseek($fh, -48, SEEK_END) or croak "seek: $!";
+                               $read = sysread($fh, $buf, 24) or
+                                   croak "read: $!";
+                               $read == 24 or
+                                   croak "read only $read bytes (!= 24)";
+                               ($last_rev, $last_commit) =
+                                   unpack(rev_map_fmt, $buf);
+                               if ($last_commit eq ('0' x40)) {
+                                       croak "inconsistent .rev_map\n";
+                               }
+                       }
+                       if ($last_rev >= $rev) {
+                               croak "last_rev is higher!: $last_rev >= $rev";
+                       }
+                       $wr_offset = -24;
                }
        }
-       sysseek($fh, 0, SEEK_END) or croak "seek: $!";
+       sysseek($fh, $wr_offset, SEEK_END) or croak "seek: $!";
        syswrite($fh, pack(rev_map_fmt, $rev, $commit), 24) == 24 or
          croak "write: $!";
 }
@@ -2599,7 +2667,7 @@ sub rev_map_set {
                                            "$db => $db_lock ($!)\n";
        }
 
-       sysopen(my $fh, $db_lock, O_RDWR | O_APPEND | O_CREAT)
+       sysopen(my $fh, $db_lock, O_RDWR | O_CREAT)
             or croak "Couldn't open $db_lock: $!\n";
        _rev_map_set($fh, $rev, $commit);
        if ($sync) {
@@ -2622,25 +2690,40 @@ sub rev_map_set {
        }
 }
 
+# If want_commit, this will return an array of (rev, commit) where
+# commit _must_ be a valid commit in the archive.
+# Otherwise, it'll return the max revision (whether or not the
+# commit is valid or just a 0x40 placeholder).
 sub rev_map_max {
-       my ($self) = @_;
+       my ($self, $want_commit) = @_;
        $self->rebuild;
        my $map_path = $self->map_path;
-       stat $map_path or return wantarray ? (0, undef) : 0;
+       stat $map_path or return $want_commit ? (0, undef) : 0;
        sysopen(my $fh, $map_path, O_RDONLY) or croak "open: $!";
        my $size = (stat($fh))[7];
        ($size % 24) == 0 or croak "inconsistent size: $size";
 
        if ($size == 0) {
                close $fh or croak "close: $!";
-               return wantarray ? (0, undef) : 0;
+               return $want_commit ? (0, undef) : 0;
        }
 
-       sysseek($fh, -24, SEEK_END);
+       sysseek($fh, -24, SEEK_END) or croak "seek: $!";
        sysread($fh, my $buf, 24) == 24 or croak "read: $!";
-       close $fh or croak "close: $!";
        my ($r, $c) = unpack(rev_map_fmt, $buf);
-       wantarray ? ($r, $c) : $r;
+       if ($want_commit && $c eq ('0' x40)) {
+               if ($size < 48) {
+                       return $want_commit ? (0, undef) : 0;
+               }
+               sysseek($fh, -48, SEEK_END) or croak "seek: $!";
+               sysread($fh, $buf, 24) == 24 or croak "read: $!";
+               ($r, $c) = unpack(rev_map_fmt, $buf);
+               if ($c eq ('0'x40)) {
+                       croak "Penultimate record is all-zeroes in $map_path";
+               }
+       }
+       close $fh or croak "close: $!";
+       $want_commit ? ($r, $c) : $r;
 }
 
 sub rev_map_get {
@@ -2672,7 +2755,7 @@ sub rev_map_get {
                        $u = $i - 24;
                } else { # $r == $rev
                        close($fh) or croak "close: $!";
-                       return $c;
+                       return $c eq ('0' x 40) ? undef : $c;
                }
        }
        close($fh) or croak "close: $!";
@@ -2997,6 +3080,20 @@ sub add_file {
 
 sub add_directory {
        my ($self, $path, $cp_path, $cp_rev) = @_;
+       my $gpath = $self->git_path($path);
+       if ($gpath eq '') {
+               my ($ls, $ctx) = command_output_pipe(qw/ls-tree
+                                                    -r --name-only -z/,
+                                                    $self->{c});
+               local $/ = "\0";
+               while (<$ls>) {
+                       chomp;
+                       $self->{gii}->remove($_);
+                       print "\tD\t$_\n" unless $::_q;
+               }
+               command_close_pipe($ls, $ctx);
+               $self->{empty}->{$path} = 0;
+       }
        my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
        delete $self->{empty}->{$dir};
        $self->{empty}->{$path} = 1;
@@ -3087,9 +3184,15 @@ sub close_file {
                }
                sysseek($fh, 0, 0) or croak $!;
                if ($fb->{mode_b} == 120000) {
-                       sysread($fh, my $buf, 5) == 5 or croak $!;
-                       $buf eq 'link ' or die "$path has mode 120000",
-                                              "but is not a link\n";
+                       eval {
+                               sysread($fh, my $buf, 5) == 5 or croak $!;
+                               $buf eq 'link ' or die "$path has mode 120000",
+                                                      " but is not a link";
+                       };
+                       if ($@) {
+                               warn "$@\n";
+                               sysseek($fh, 0, 0) or croak $!;
+                       }
                }
                defined(my $pid = open my $out,'-|') or die "Can't fork: $!\n";
                if (!$pid) {
@@ -3529,6 +3632,7 @@ sub _auth_providers () {
          SVN::Client::get_ssl_client_cert_file_provider(),
          SVN::Client::get_ssl_client_cert_prompt_provider(
            \&Git::SVN::Prompt::ssl_client_cert, 2),
+         SVN::Client::get_ssl_client_cert_pw_file_provider(),
          SVN::Client::get_ssl_client_cert_pw_prompt_provider(
            \&Git::SVN::Prompt::ssl_client_cert_pw, 2),
          SVN::Client::get_username_provider(),
@@ -3849,6 +3953,7 @@ sub gs_fetch_loop_common {
                                if ($log_entry) {
                                        $gs->do_git_commit($log_entry);
                                }
+                               $INDEX_FILES{$gs->{index}} = 1;
                        }
                        foreach my $g (@$globs) {
                                my $k = "svn-remote.$g->{remote}." .
@@ -3863,6 +3968,13 @@ sub gs_fetch_loop_common {
                                $ra_invalid = undef;
                        }
                }
+               # pre-fill the .rev_db since it'll eventually get filled in
+               # with '0' x40 if something new gets committed
+               foreach my $gs (@$gsv) {
+                       next if $gs->rev_map_max >= $max;
+                       next if defined $gs->rev_map_get($max);
+                       $gs->rev_map_set($max, 0 x40);
+               }
                foreach my $g (@$globs) {
                        my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
                        Git::SVN::tmp_config($k, $max);
@@ -3979,6 +4091,10 @@ sub skip_unknown_revs {
                        warn "W: Ignoring error from SVN, path probably ",
                             "does not exist: ($errno): ",
                             $err->expanded_message,"\n";
+                       warn "W: Do not be alarmed at the above message ",
+                            "git-svn is just searching aggressively for ",
+                            "old history.\n",
+                            "This may take a while on large repositories\n";
                        $ignored_err{$err_key} = 1;
                }
                return;
@@ -4037,39 +4153,7 @@ sub cmt_showable {
 }
 
 sub log_use_color {
-       return 1 if $color;
-       my ($dc, $dcvar);
-       $dcvar = 'color.diff';
-       $dc = `git-config --get $dcvar`;
-       if ($dc eq '') {
-               # nothing at all; fallback to "diff.color"
-               $dcvar = 'diff.color';
-               $dc = `git-config --get $dcvar`;
-       }
-       chomp($dc);
-       if ($dc eq 'auto') {
-               my $pc;
-               $pc = `git-config --get color.pager`;
-               if ($pc eq '') {
-                       # does not have it -- fallback to pager.color
-                       $pc = `git-config --bool --get pager.color`;
-               }
-               else {
-                       $pc = `git-config --bool --get color.pager`;
-                       if ($?) {
-                               $pc = 'false';
-                       }
-               }
-               chomp($pc);
-               if (-t *STDOUT || (defined $pager && $pc eq 'true')) {
-                       return ($ENV{TERM} && $ENV{TERM} ne 'dumb');
-               }
-               return 0;
-       }
-       return 0 if $dc eq 'never';
-       return 1 if $dc eq 'always';
-       chomp($dc = `git-config --bool --get $dcvar`);
-       return ($dc eq 'true');
+       return $color || Git->repository->get_colorbool('color.diff');
 }
 
 sub git_svn_log_cmd {
@@ -4128,6 +4212,7 @@ sub config_pager {
        } elsif (length $pager == 0 || $pager eq 'cat') {
                $pager = undef;
        }
+       $ENV{GIT_PAGER_IN_USE} = defined($pager);
 }
 
 sub run_pager {