git-svn: add 'clone' command, an alias for init + fetch
[gitweb.git] / git-svn.perl
index eca08bdd3b16fdec0392e177d3e36dc4e5ecbdf6..2cc7c33381fe5394f08eb88e720e412ebe63ffb5 100755 (executable)
@@ -90,6 +90,9 @@ BEGIN
                        { 'revision|r=s' => \$_revision,
                          'fetch-all|all' => \$_fetch_all,
                           %fc_opts } ],
+       clone => [ \&cmd_clone, "Initialize and fetch revisions",
+                       { 'revision|r=s' => \$_revision,
+                          %fc_opts, %init_opts } ],
        init => [ \&cmd_init, "Initialize a repo for tracking" .
                          " (requires URL argument)",
                          \%init_opts ],
@@ -167,7 +170,7 @@ BEGIN
 version() if $_version;
 usage(1) unless defined $cmd;
 load_authors() if $_authors;
-unless ($cmd =~ /^(?:init|multi-init|commit-diff)$/) {
+unless ($cmd =~ /^(?:clone|init|multi-init|commit-diff)$/) {
        Git::SVN::Migration::migration_check();
 }
 Git::SVN::init_vars();
@@ -237,6 +240,22 @@ sub init_subdir {
        $ENV{GIT_DIR} = $repo_path . "/.git";
 }
 
+sub cmd_clone {
+       my ($url, $path) = @_;
+       if (!defined $path &&
+           (defined $_trunk || defined $_branches || defined $_tags) &&
+           $url !~ m#^[a-z\+]+://#) {
+               $path = $url;
+       }
+       warn "--path: $path\n" if defined $path;
+       $path = basename($url) if !defined $path || !length $path;
+       warn "++path: $path\n" if defined $path;
+       mkpath([$path]);
+       chdir $path or die "Couldn't chdir to $path\n";
+       cmd_init(@_);
+       Git::SVN::fetch_all($Git::SVN::default_repo_id);
+}
+
 sub cmd_init {
        if (defined $_trunk || defined $_branches || defined $_tags) {
                return cmd_multi_init(@_);
@@ -844,6 +863,8 @@ sub fetch_all {
                                         "svn-remote.$repo_id.${t}-maxRev") };
                if (defined $max_rev && ($max_rev < $base)) {
                        $base = $max_rev;
+               } elsif (!defined $max_rev) {
+                       $base = 0;
                }
        }
 
@@ -1066,10 +1087,7 @@ sub new {
        $self->{url} = command_oneline('config', '--get',
                                       "svn-remote.$repo_id.url") or
                   die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
-       if ((-z $self->db_path || ! -e $self->db_path) &&
-           ::verify_ref($self->refname.'^0')) {
-               $self->rebuild;
-       }
+       $self->rebuild;
        $self;
 }
 
@@ -1287,7 +1305,12 @@ sub get_fetch_range {
 
 sub tmp_config {
        my (@args) = @_;
-       my $config = "$ENV{GIT_DIR}/svn/config";
+       my $old_def_config = "$ENV{GIT_DIR}/svn/config";
+       my $config = "$ENV{GIT_DIR}/svn/.metadata";
+       if (-e $old_def_config && ! -e $config) {
+               rename $old_def_config, $config or
+                      die "Failed rename $old_def_config => $config: $!\n";
+       }
        my $old_config = $ENV{GIT_CONFIG};
        $ENV{GIT_CONFIG} = $config;
        $@ = undef;
@@ -1737,6 +1760,8 @@ sub set_tree {
 sub rebuild {
        my ($self) = @_;
        my $db_path = $self->db_path;
+       return if (-e $db_path && ! -z $db_path);
+       return unless ::verify_ref($self->refname.'^0');
        if (-f $self->{db_root}) {
                rename $self->{db_root}, $db_path or die
                     "rename $self->{db_root} => $db_path failed: $!\n";
@@ -1863,6 +1888,7 @@ sub rev_db_set {
 
 sub rev_db_max {
        my ($self) = @_;
+       $self->rebuild;
        my $db_path = $self->db_path;
        my @stat = stat $db_path or return 0;
        ($stat[7] % 41) == 0 or die "$db_path inconsistent size: $stat[7]\n";