Prepare Git::SVN for extraction into its own file.
[gitweb.git] / git-svn.perl
index 6673d21f841c7f5a4806bc7785c5ae7755f9297b..2e1eb84dd1821c0f96517f34ed6fabd6c293f21b 100755 (executable)
@@ -10,6 +10,8 @@
 $AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
 $VERSION = '@@GIT_VERSION@@';
 
+use Git::SVN::Utils qw(fatal can_compress);
+
 # From which subdir have we been invoked?
 my $cmd_dir_prefix = eval {
        command_oneline([qw/rev-parse --show-prefix/], STDERR => 0)
@@ -35,8 +37,6 @@
 $ENV{TZ} = 'UTC';
 $| = 1; # unbuffer STDOUT
 
-sub fatal (@) { print STDERR "@_\n"; exit 1 }
-
 # All SVN commands do it.  Otherwise we may die on SIGPIPE when the remote
 # repository decides to close the connection which we expect to be kept alive.
 $SIG{PIPE} = 'IGNORE';
@@ -66,7 +66,7 @@ sub _req_svn {
                fatal "Need SVN::Core 1.1.0 or better (got $SVN::Core::VERSION)";
        }
 }
-my $can_compress = eval { require Compress::Zlib; 1};
+
 use Carp qw/croak/;
 use Digest::MD5;
 use IO::File qw//;
@@ -89,7 +89,7 @@ BEGIN
        foreach (qw/command command_oneline command_noisy command_output_pipe
                    command_input_pipe command_close_pipe
                    command_bidi_pipe command_close_bidi_pipe/) {
-               for my $package ( qw(Git::SVN::Migration Git::SVN::Log Git::SVN),
+               for my $package ( qw(Git::SVN::Migration Git::SVN::Log),
                        __PACKAGE__) {
                        *{"${package}::$_"} = \&{"Git::$_"};
                }
@@ -109,6 +109,10 @@ BEGIN
        $_merge, $_strategy, $_preserve_merges, $_dry_run, $_local,
        $_prefix, $_no_checkout, $_url, $_verbose,
        $_git_format, $_commit_url, $_tag, $_merge_info, $_interactive);
+
+# This is a refactoring artifact so Git::SVN can get at this git-svn switch.
+sub opt_prefix { return $_prefix || '' }
+
 $Git::SVN::_follow_parent = 1;
 $Git::SVN::Fetcher::_placeholder_filename = ".gitignore";
 $_q ||= 0;
@@ -1578,7 +1582,7 @@ sub cmd_reset {
 }
 
 sub cmd_gc {
-       if (!$can_compress) {
+       if (!can_compress()) {
                warn "Compress::Zlib could not be found; unhandled.log " .
                     "files will not be compressed.\n";
        }
@@ -2013,13 +2017,13 @@ sub md5sum {
        } elsif (!$ref) {
                $md5->add($arg) or croak $!;
        } else {
-               ::fatal "Can't provide MD5 hash for unknown ref type: '", $ref, "'";
+               fatal "Can't provide MD5 hash for unknown ref type: '", $ref, "'";
        }
        return $md5->hexdigest();
 }
 
 sub gc_directory {
-       if ($can_compress && -f $_ && basename($_) eq "unhandled.log") {
+       if (can_compress() && -f $_ && basename($_) eq "unhandled.log") {
                my $out_filename = $_ . ".gz";
                open my $in_fh, "<", $_ or die "Unable to open $_: $!\n";
                binmode $in_fh;
@@ -2037,6 +2041,7 @@ sub gc_directory {
        }
 }
 
+{
 package Git::SVN;
 use strict;
 use warnings;
@@ -2054,6 +2059,16 @@ package Git::SVN;
 use Memoize;  # core since 5.8.0, Jul 2002
 use Memoize::Storable;
 use POSIX qw(:signal_h);
+
+use Git qw(
+    command
+    command_oneline
+    command_noisy
+    command_output_pipe
+    command_close_pipe
+);
+use Git::SVN::Utils qw(fatal can_compress);
+
 my $can_use_yaml;
 BEGIN {
        $can_use_yaml = eval { require Git::SVN::Memoize::YAML; 1};
@@ -2879,8 +2894,8 @@ sub assert_index_clean {
                command_noisy('read-tree', $treeish);
                $x = command_oneline('write-tree');
                if ($y ne $x) {
-                       ::fatal "trees ($treeish) $y != $x\n",
-                               "Something is seriously wrong...";
+                       fatal "trees ($treeish) $y != $x\n",
+                             "Something is seriously wrong...";
                }
        });
 }
@@ -3235,7 +3250,7 @@ sub mkemptydirs {
        my %empty_dirs = ();
        my $gz_file = "$self->{dir}/unhandled.log.gz";
        if (-f $gz_file) {
-               if (!$can_compress) {
+               if (!can_compress()) {
                        warn "Compress::Zlib could not be found; ",
                             "empty directories in $gz_file will not be read\n";
                } else {
@@ -3918,7 +3933,7 @@ sub set_tree {
        my ($self, $tree) = (shift, shift);
        my $log_entry = ::get_commit_entry($tree);
        unless ($self->{last_rev}) {
-               ::fatal("Must have an existing revision to commit");
+               fatal("Must have an existing revision to commit");
        }
        my %ed_opts = ( r => $self->{last_rev},
                        log => $log_entry->{log},
@@ -4276,12 +4291,13 @@ sub find_rev_after {
 sub _new {
        my ($class, $repo_id, $ref_id, $path) = @_;
        unless (defined $repo_id && length $repo_id) {
-               $repo_id = $Git::SVN::default_repo_id;
+               $repo_id = $default_repo_id;
        }
        unless (defined $ref_id && length $ref_id) {
-               $_prefix = '' unless defined($_prefix);
+               # Access the prefix option from the git-svn main program if it's loaded.
+               my $prefix = defined &::opt_prefix ? ::opt_prefix() : "";
                $_[2] = $ref_id =
-                            "refs/remotes/$_prefix$Git::SVN::default_ref_id";
+                            "refs/remotes/$prefix$default_ref_id";
        }
        $_[1] = $repo_id;
        my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
@@ -4343,10 +4359,12 @@ sub uri_decode {
 sub remove_username {
        $_[0] =~ s{^([^:]*://)[^@]+@}{$1};
 }
+}
 
 package Git::SVN::Log;
 use strict;
 use warnings;
+use Git::SVN::Utils qw(fatal);
 use POSIX qw/strftime/;
 use constant commit_log_separator => ('-' x 72) . "\n";
 use vars qw/$TZ $limit $color $pager $non_recursive $verbose $oneline
@@ -4445,15 +4463,15 @@ sub config_pager {
 sub run_pager {
        return unless defined $pager;
        pipe my ($rfd, $wfd) or return;
-       defined(my $pid = fork) or ::fatal "Can't fork: $!";
+       defined(my $pid = fork) or fatal "Can't fork: $!";
        if (!$pid) {
                open STDOUT, '>&', $wfd or
-                                    ::fatal "Can't redirect to stdout: $!";
+                                    fatal "Can't redirect to stdout: $!";
                return;
        }
-       open STDIN, '<&', $rfd or ::fatal "Can't redirect stdin: $!";
+       open STDIN, '<&', $rfd or fatal "Can't redirect stdin: $!";
        $ENV{LESS} ||= 'FRSX';
-       exec $pager or ::fatal "Can't run pager: $! ($pager)";
+       exec $pager or fatal "Can't run pager: $! ($pager)";
 }
 
 sub format_svn_date {
@@ -4602,7 +4620,7 @@ sub cmd_show_log {
                } elsif ($::_revision =~ /^\d+$/) {
                        $r_min = $r_max = $::_revision;
                } else {
-                       ::fatal "-r$::_revision is not supported, use ",
+                       fatal "-r$::_revision is not supported, use ",
                                "standard 'git log' arguments instead";
                }
        }