fix importing of subversion tars
[gitweb.git] / git-svn.perl
index 6216cade0f0e019457293aff0e1561c4bc0e42f2..efc4c88a4ea158bd630a6f53a8346e344b360ed6 100755 (executable)
@@ -168,14 +168,14 @@ BEGIN
 my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
 
 read_repo_config(\%opts);
-Getopt::Long::Configure('pass_through') if $cmd eq 'log';
+Getopt::Long::Configure('pass_through') if ($cmd && $cmd eq 'log');
 my $rv = GetOptions(%opts, 'help|H|h' => \$_help, 'version|V' => \$_version,
                     'minimize-connections' => \$Git::SVN::Migration::_minimize,
                     'id|i=s' => \$Git::SVN::default_ref_id,
                     'svn-remote|remote|R=s' => sub {
                        $Git::SVN::no_reuse_existing = 1;
                        $Git::SVN::default_repo_id = $_[1] });
-exit 1 if (!$rv && $cmd ne 'log');
+exit 1 if (!$rv && $cmd && $cmd ne 'log');
 
 usage(0) if $_help;
 version() if $_version;
@@ -363,13 +363,12 @@ sub cmd_dcommit {
        my $head = shift;
        $head ||= 'HEAD';
        my @refs;
-       my ($url, $rev, $uuid) = working_head_info($head, \@refs);
-       my $c = $refs[-1];
-       unless (defined $url && defined $rev && defined $uuid) {
+       my ($url, $rev, $uuid, $gs) = working_head_info($head, \@refs);
+       unless ($gs) {
                die "Unable to determine upstream SVN information from ",
                    "$head history\n";
        }
-       my $gs = Git::SVN->find_by_url($url);
+       my $c = $refs[-1];
        my $last_rev;
        foreach my $d (@refs) {
                if (!verify_ref("$d~1")) {
@@ -431,16 +430,11 @@ sub cmd_dcommit {
 
 sub cmd_rebase {
        command_noisy(qw/update-index --refresh/);
-       my $url = (working_head_info('HEAD'))[0];
-       if (!defined $url) {
+       my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
+       unless ($gs) {
                die "Unable to determine upstream SVN information from ",
                    "working tree history\n";
        }
-
-       my $gs = Git::SVN->find_by_url($url);
-       unless ($gs) {
-               die "Unable to determine remote information from URL: $url\n";
-       }
        if (command(qw/diff-index HEAD --/)) {
                print STDERR "Cannot rebase with uncommited changes:\n";
                command_noisy('status');
@@ -453,8 +447,8 @@ sub cmd_rebase {
 }
 
 sub cmd_show_ignore {
-       my $url = (::working_head_info('HEAD'))[0];
-       my $gs = Git::SVN->find_by_url($url) || Git::SVN->new;
+       my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
+       $gs ||= Git::SVN->new;
        my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
        $gs->traverse_ignore(\*STDOUT, $gs->{path}, $r);
 }
@@ -776,16 +770,23 @@ sub cmt_metadata {
 
 sub working_head_info {
        my ($head, $refs) = @_;
-       my ($url, $rev, $uuid);
        my ($fh, $ctx) = command_output_pipe('rev-list', $head);
        while (<$fh>) {
                chomp;
-               ($url, $rev, $uuid) = cmt_metadata($_);
-               last if (defined $url && defined $rev && defined $uuid);
+               my ($url, $rev, $uuid) = cmt_metadata($_);
+               if (defined $url && defined $rev) {
+                       if (my $gs = Git::SVN->find_by_url($url)) {
+                               my $c = $gs->rev_db_get($rev);
+                               if ($c && $c eq $_) {
+                                       close $fh; # break the pipe
+                                       return ($url, $rev, $uuid, $gs);
+                               }
+                       }
+               }
                unshift @$refs, $_ if $refs;
        }
-       close $fh; # break the pipe
-       ($url, $rev, $uuid);
+       command_close_pipe($fh, $ctx);
+       (undef, undef, undef, undef);
 }
 
 package Git::SVN;
@@ -1681,7 +1682,10 @@ sub find_parent_branch {
        }
        my ($r0, $parent) = $gs->find_rev_before($r, 1);
        if (!defined $r0 || !defined $parent) {
-               $gs->fetch(0, $r);
+               my ($base, $head) = parse_revision_argument(0, $r);
+               if ($base <= $r) {
+                       $gs->fetch($base, $r);
+               }
                ($r0, $parent) = $gs->last_rev_commit;
        }
        if (defined $r0 && defined $parent) {
@@ -3158,6 +3162,8 @@ sub match_globs {
                        my $p = $1;
                        my $pathname = $g->{path}->full_path($p);
                        next if $exists->{$pathname};
+                       next if ($self->check_path($pathname, $r) !=
+                                $SVN::Node::dir);
                        $exists->{$pathname} = Git::SVN->init(
                                              $self->{url}, $pathname, undef,
                                              $g->{ref}->full_path($p), 1);
@@ -3262,12 +3268,19 @@ package Git::SVN::Log;
 sub cmt_showable {
        my ($c) = @_;
        return 1 if defined $c->{r};
+
+       # big commit message got truncated by the 16k pretty buffer in rev-list
        if ($c->{l} && $c->{l}->[-1] eq "...\n" &&
                                $c->{a_raw} =~ /\@([a-f\d\-]+)>$/) {
+               @{$c->{l}} = ();
                my @log = command(qw/cat-file commit/, $c->{c});
-               shift @log while ($log[0] ne "\n");
+
+               # shift off the headers
+               shift @log while ($log[0] ne '');
                shift @log;
-               @{$c->{l}} = grep !/^git-svn-id: /, @log;
+
+               # TODO: make $c->{l} not have a trailing newline in the future
+               @{$c->{l}} = map { "$_\n" } grep !/^git-svn-id: /, @log;
 
                (undef, $c->{r}, undef) = ::extract_metadata(
                                (grep(/^git-svn-id: /, @log))[-1]);
@@ -3321,8 +3334,8 @@ sub git_svn_log_cmd {
                last;
        }
 
-       my $url = (::working_head_info($head))[0];
-       my $gs = Git::SVN->find_by_url($url) || Git::SVN->_new;
+       my ($url, $rev, $uuid, $gs) = ::working_head_info($head);
+       $gs ||= Git::SVN->_new;
        my @cmd = (qw/log --abbrev-commit --pretty=raw --default/,
                   $gs->refname);
        push @cmd, '-r' unless $non_recursive;