Code

Merge branch 'master' into db/fetch-pack
[git.git] / git-svn.perl
index d21eb7fa9e32b1b41943b5bc24e84928c41edf96..2c8a1580f8495b0f158802f8513a5df50aca3a02 100755 (executable)
@@ -59,7 +59,7 @@ my ($_stdin, $_help, $_edit,
        $_template, $_shared,
        $_version, $_fetch_all, $_no_rebase,
        $_merge, $_strategy, $_dry_run, $_local,
-       $_prefix, $_no_checkout, $_verbose, $_first_parent);
+       $_prefix, $_no_checkout, $_verbose);
 $Git::SVN::_follow_parent = 1;
 my %remote_opts = ( 'username=s' => \$Git::SVN::Prompt::_username,
                     'config-dir=s' => \$Git::SVN::Ra::config_dir,
@@ -119,14 +119,12 @@ my %cmd = (
                          'dry-run|n' => \$_dry_run,
                          'fetch-all|all' => \$_fetch_all,
                          'no-rebase' => \$_no_rebase,
-                         'first-parent' => \$_first_parent,
                        %cmt_opts, %fc_opts } ],
        'set-tree' => [ \&cmd_set_tree,
                        "Set an SVN repository to a git tree-ish",
                        { 'stdin|' => \$_stdin, %cmt_opts, %fc_opts, } ],
        'show-ignore' => [ \&cmd_show_ignore, "Show svn:ignore listings",
-                       { 'revision|r=i' => \$_revision,
-                         'first-parent' => \$_first_parent
+                       { 'revision|r=i' => \$_revision
                        } ],
        'multi-fetch' => [ \&cmd_multi_fetch,
                           "Deprecated alias for $0 fetch --all",
@@ -147,20 +145,16 @@ my %cmd = (
                          'non-recursive' => \$Git::SVN::Log::non_recursive,
                          'authors-file|A=s' => \$_authors,
                          'color' => \$Git::SVN::Log::color,
-                         'pager=s' => \$Git::SVN::Log::pager,
-                         'first-parent' => \$_first_parent
+                         'pager=s' => \$Git::SVN::Log::pager
                        } ],
        'find-rev' => [ \&cmd_find_rev, "Translate between SVN revision numbers and tree-ish",
-                       {
-                         'first-parent' => \$_first_parent
-                       } ],
+                       {} ],
        'rebase' => [ \&cmd_rebase, "Fetch and rebase your working directory",
                        { 'merge|m|M' => \$_merge,
                          'verbose|v' => \$_verbose,
                          'strategy|s=s' => \$_strategy,
                          'local|l' => \$_local,
                          'fetch-all|all' => \$_fetch_all,
-                         'first-parent' => \$_first_parent,
                          %fc_opts } ],
        'commit-diff' => [ \&cmd_commit_diff,
                           'Commit a diff between two trees',
@@ -818,8 +812,7 @@ sub cmt_metadata {
 
 sub working_head_info {
        my ($head, $refs) = @_;
-       my @args = ('log', '--no-color');
-       push @args, '--first-parent' if $_first_parent;
+       my @args = ('log', '--no-color', '--first-parent');
        my ($fh, $ctx) = command_output_pipe(@args, $head);
        my $hash;
        my %max;
@@ -850,14 +843,9 @@ sub working_head_info {
 
 sub read_commit_parents {
        my ($parents, $c) = @_;
-       my ($fh, $ctx) = command_output_pipe(qw/cat-file commit/, $c);
-       while (<$fh>) {
-               chomp;
-               last if '';
-               /^parent ($sha1)/ or next;
-               push @{$parents->{$c}}, $1;
-       }
-       close $fh; # break the pipe
+       chomp(my $p = command_oneline(qw/rev-list --parents -1/, $c));
+       $p =~ s/^($c)\s*// or die "rev-list --parents -1 $c failed!\n";
+       @{$parents->{$c}} = split(/ /, $p);
 }
 
 sub linearize_history {
@@ -1859,6 +1847,16 @@ sub find_parent_branch {
                        $gs->ra->gs_do_switch($r0, $rev, $gs,
                                              $self->full_url, $ed)
                          or die "SVN connection failed somewhere...\n";
+               } elsif ($self->ra->trees_match($new_url, $r0,
+                                               $self->full_url, $rev)) {
+                       print STDERR "Trees match:\n",
+                                    "  $new_url\@$r0\n",
+                                    "  ${\$self->full_url}\@$rev\n",
+                                    "Following parent with no changes\n";
+                       $self->tmp_index_do(sub {
+                           command_noisy('read-tree', $parent);
+                       });
+                       $self->{last_commit} = $parent;
                } else {
                        print STDERR "Following parent with do_update\n";
                        $ed = SVN::Git::Fetcher->new($self);
@@ -3022,7 +3020,7 @@ package Git::SVN::Ra;
 use vars qw/@ISA $config_dir $_log_window_size/;
 use strict;
 use warnings;
-my ($can_do_switch, %ignored_err, $RA);
+my ($ra_invalid, $can_do_switch, %ignored_err, $RA);
 
 BEGIN {
        # enforce temporary pool usage for some simple functions
@@ -3039,30 +3037,57 @@ BEGIN {
        }
 }
 
+sub _auth_providers () {
+       [
+         SVN::Client::get_simple_provider(),
+         SVN::Client::get_ssl_server_trust_file_provider(),
+         SVN::Client::get_simple_prompt_provider(
+           \&Git::SVN::Prompt::simple, 2),
+         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_prompt_provider(
+           \&Git::SVN::Prompt::ssl_client_cert_pw, 2),
+         SVN::Client::get_username_provider(),
+         SVN::Client::get_ssl_server_trust_prompt_provider(
+           \&Git::SVN::Prompt::ssl_server_trust),
+         SVN::Client::get_username_prompt_provider(
+           \&Git::SVN::Prompt::username, 2)
+       ]
+}
+
 sub new {
        my ($class, $url) = @_;
        $url =~ s!/+$!!;
        return $RA if ($RA && $RA->{url} eq $url);
 
        SVN::_Core::svn_config_ensure($config_dir, undef);
-       my ($baton, $callbacks) = SVN::Core::auth_open_helper([
-           SVN::Client::get_simple_provider(),
-           SVN::Client::get_ssl_server_trust_file_provider(),
-           SVN::Client::get_simple_prompt_provider(
-             \&Git::SVN::Prompt::simple, 2),
-           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_prompt_provider(
-             \&Git::SVN::Prompt::ssl_client_cert_pw, 2),
-           SVN::Client::get_username_provider(),
-           SVN::Client::get_ssl_server_trust_prompt_provider(
-             \&Git::SVN::Prompt::ssl_server_trust),
-           SVN::Client::get_username_prompt_provider(
-             \&Git::SVN::Prompt::username, 2),
-         ]);
+       my ($baton, $callbacks) = SVN::Core::auth_open_helper(_auth_providers);
        my $config = SVN::Core::config_get_config($config_dir);
        $RA = undef;
+       my $dont_store_passwords = 1;
+       my $conf_t = ${$config}{'config'};
+       {
+               # The usage of $SVN::_Core::SVN_CONFIG_* variables
+               # produces warnings that variables are used only once.
+               # I had not found the better way to shut them up, so
+               # warnings are disabled in this block.
+               no warnings;
+               if (SVN::_Core::svn_config_get_bool($conf_t,
+                   $SVN::_Core::SVN_CONFIG_SECTION_AUTH,
+                   $SVN::_Core::SVN_CONFIG_OPTION_STORE_PASSWORDS,
+                   1) == 0) {
+                       SVN::_Core::svn_auth_set_parameter($baton,
+                           $SVN::_Core::SVN_AUTH_PARAM_DONT_STORE_PASSWORDS,
+                           bless (\$dont_store_passwords, "_p_void"));
+               }
+               if (SVN::_Core::svn_config_get_bool($conf_t,
+                   $SVN::_Core::SVN_CONFIG_SECTION_AUTH,
+                   $SVN::_Core::SVN_CONFIG_OPTION_STORE_AUTH_CREDS,
+                   1) == 0) {
+                       $Git::SVN::Prompt::_no_auth_cache = 1;
+               }
+       }
        my $self = SVN::Ra->new(url => $url, auth => $baton,
                              config => $config,
                              pool => SVN::Pool->new,
@@ -3124,6 +3149,24 @@ sub get_log {
        $ret;
 }
 
+sub trees_match {
+       my ($self, $url1, $rev1, $url2, $rev2) = @_;
+       my $ctx = SVN::Client->new(auth => _auth_providers);
+       my $out = IO::File->new_tmpfile;
+
+       # older SVN (1.1.x) doesn't take $pool as the last parameter for
+       # $ctx->diff(), so we'll create a default one
+       my $pool = SVN::Pool->new_default_sub;
+
+       $ra_invalid = 1; # this will open a new SVN::Ra connection to $url1
+       $ctx->diff([], $url1, $rev1, $url2, $rev2, 1, 1, 0, $out, $out);
+       $out->flush;
+       my $ret = (($out->stat)[7] == 0);
+       close $out or croak $!;
+
+       $ret;
+}
+
 sub get_commit_editor {
        my ($self, $log, $cb, $pool) = @_;
        my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
@@ -3183,7 +3226,11 @@ sub gs_do_switch {
                        $self->{url} = $full_url;
                        $reparented = 1;
                } else {
+                       $_[0] = undef;
+                       $self = undef;
+                       $RA = undef;
                        $ra = Git::SVN::Ra->new($full_url);
+                       $ra_invalid = 1;
                }
        }
        $ra ||= $self;
@@ -3243,6 +3290,7 @@ sub gs_fetch_loop_common {
        my $inc = $_log_window_size;
        my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc);
        my $longest_path = longest_common_path($gsv, $globs);
+       my $ra_url = $self->{url};
        while (1) {
                my %revs;
                my $err;
@@ -3304,6 +3352,13 @@ sub gs_fetch_loop_common {
                                        "$g->{t}-maxRev";
                                Git::SVN::tmp_config($k, $r);
                        }
+                       if ($ra_invalid) {
+                               $_[0] = undef;
+                               $self = undef;
+                               $RA = undef;
+                               $self = Git::SVN::Ra->new($ra_url);
+                               $ra_invalid = undef;
+                       }
                }
                # pre-fill the .rev_db since it'll eventually get filled in
                # with '0' x40 if something new gets committed
@@ -3578,7 +3633,7 @@ sub config_pager {
 }
 
 sub run_pager {
-       return unless -t *STDOUT;
+       return unless -t *STDOUT && defined $pager;
        pipe my $rfd, my $wfd or return;
        defined(my $pid = fork) or ::fatal "Can't fork: $!\n";
        if (!$pid) {