git-svn: simplify the (multi-)init methods of fetching
Also, some changes to avoid creating dead dirs under .git/svn/. We now create all directories as late as possible. Signed-off-by: Eric Wong <normalperson@yhbt.net>
Eric Wong committed
Feb 14, 2007 at 15:10 UTC
b4d57e5ea3349a6bf63c3626f6fb36bac11be3c0
1 file changed
+35
-51
git-svn.perl
+35
-51
@@ -487,48 +487,24 @@ sub complete_url_ls_init {
487
"and a separate URL is not specified\n");
488
}
489
}
490
- my $r = defined $_revision ? $_revision : $ra->get_latest_revnum;
491
- my ($dirent, undef, undef) = $ra->get_dir($repo_path, $r);
490
my $url = $ra->{url};
493
- my $remote_id;
494
- my $remote_path;
495
- foreach my $d (sort keys %$dirent) {
496
- next if ($dirent->{$d}->kind != $SVN::Node::dir);
497
- my $path = "$repo_path/$d";
498
- my $ref = "$pfx$d";
499
- my $gs = eval { Git::SVN->new($ref) };
500
- # don't try to init already existing refs
501
- unless ($gs) {
502
- print "init $url/$path => $ref\n";
503
- $gs = Git::SVN->init($url, $path, undef, $ref, 1);
504
- }
505
- if ($gs) {
506
- my $k = "svn-remote.$gs->{repo_id}.url";
507
- my $orig_url = eval {
508
- command_oneline(qw/config --get/, $k)
509
- };
510
- if ($orig_url && ($orig_url ne $gs->{url})) {
511
- die "$k already set: $orig_url\n",
512
- "wanted to set to: $gs->{url}\n";
513
- }
514
- unless ($orig_url) {
515
- command_oneline('config', $k, $gs->{url});
516
- }
517
- $remote_id = $gs->{repo_id};
518
- last;
519
- }
491
+ my $gs = Git::SVN->init($url, undef, undef, undef, 1);
492
+ my $k = "svn-remote.$gs->{repo_id}.url";
493
+ my $orig_url = eval { command_oneline(qw/config --get/, $k) };
494
+ if ($orig_url && ($orig_url ne $gs->{url})) {
495
+ die "$k already set: $orig_url\n",
496
+ "wanted to set to: $gs->{url}\n";
497
}
521
- if (defined $remote_id) {
522
- $remote_path = "$ra->{svn_path}/$repo_path/*";
523
- $remote_path =~ s#/+#/#g;
524
- $remote_path =~ s#^/##g;
525
- my ($n) = ($switch =~ /^--(\w+)/);
526
- if (length $pfx && $pfx !~ m#/$#) {
527
- die "--prefix='$pfx' must have a trailing slash '/'\n";
528
- }
529
- command_noisy('config', "svn-remote.$remote_id.$n",
530
- "$remote_path:refs/remotes/$pfx*");
498
+ command_oneline('config', $k, $gs->{url}) unless $orig_url;
499
+ my $remote_path = "$ra->{svn_path}/$repo_path/*";
500
+ $remote_path =~ s#/+#/#g;
501
+ $remote_path =~ s#^/##g;
502
+ my ($n) = ($switch =~ /^--(\w+)/);
503
+ if (length $pfx && $pfx !~ m#/$#) {
504
+ die "--prefix='$pfx' must have a trailing slash '/'\n";
505
}
506
+ command_noisy('config', "svn-remote.$gs->{repo_id}.$n",
507
+ "$remote_path:refs/remotes/$pfx*");
508
}
509
510
sub verify_ref {
@@ -1236,19 +1212,23 @@ sub get_fetch_range {
1212
sub tmp_config {
1213
my (@args) = @_;
1214
my $config = "$ENV{GIT_DIR}/svn/config";
1239
- unless (-f $config) {
1240
- open my $fh, '>', $config or
1241
- die "Can't open $config: $!\n";
1242
- print $fh "; This file is used internally by git-svn\n" or
1243
- die "Couldn't write to $config: $!\n";
1244
- print $fh "; You should not have to edit it\n" or
1245
- die "Couldn't write to $config: $!\n";
1246
- close $fh or die "Couldn't close $config: $!\n";
1247
- }
1215
my $old_config = $ENV{GIT_CONFIG};
1216
$ENV{GIT_CONFIG} = $config;
1217
$@ = undef;
1251
- my @ret = eval { command('config', @args) };
1218
+ my @ret = eval {
1219
+ unless (-f $config) {
1220
+ mkfile($config);
1221
+ open my $fh, '>', $config or
1222
+ die "Can't open $config: $!\n";
1223
+ print $fh "; This file is used internally by ",
1224
+ "git-svn\n" or die
1225
+ "Couldn't write to $config: $!\n";
1226
+ print $fh "; You should not have to edit it\n" or
1227
+ die "Couldn't write to $config: $!\n";
1228
+ close $fh or die "Couldn't close $config: $!\n";
1229
+ }
1230
+ command('config', @args);
1231
+ };
1232
my $err = $@;
1233
if (defined $old_config) {
1234
$ENV{GIT_CONFIG} = $old_config;
@@ -1264,7 +1244,11 @@ sub tmp_index_do {
1244
my $old_index = $ENV{GIT_INDEX_FILE};
1245
$ENV{GIT_INDEX_FILE} = $self->{index};
1246
$@ = undef;
1267
- my @ret = eval { &$sub };
1247
+ my @ret = eval {
1248
+ my ($dir, $base) = ($self->{index} =~ m#^(.*?)/?([^/]+)$#);
1249
+ mkpath([$dir]) unless -d $dir;
1250
+ &$sub;
1251
+ };
1252
my $err = $@;
1253
if (defined $old_index) {
1254
$ENV{GIT_INDEX_FILE} = $old_index;
@@ -1846,7 +1830,7 @@ sub _new {
1830
$_[1] = $repo_id = sanitize_remote_name($repo_id);
1831
my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
1832
$_[3] = $path = '' unless (defined $path);
1849
- mkpath([$dir]);
1833
+ mkpath(["$ENV{GIT_DIR}/svn"]);
1834
bless {
1835
ref_id => $ref_id, dir => $dir, index => "$dir/index",
1836
path => $path, config => "$ENV{GIT_DIR}/svn/config",