From e7db67e6f18495332c4d688d3291b05851526a6e Mon Sep 17 00:00:00 2001
From: Eric Wong <normalperson@yhbt.net>
Date: Thu, 11 Jan 2007 17:09:26 -0800
Subject: [PATCH] git-svn: make multi-init capable of reusing the Ra connection

If a user specified a seperate URL and --tags/--branches as
a sepearte URL, allow the Ra object (and therefore the connection)
to be reused.

We'll get rid of libsvn_ls_fullurl() since it was only used
in one place.

Signed-off-by: Eric Wong <normalperson@yhbt.net>
---
 git-svn.perl                      | 50 ++++++++++++++-----------------
 t/t9103-git-svn-graft-branches.sh |  6 ++++
 2 files changed, 29 insertions(+), 27 deletions(-)

diff --git a/git-svn.perl b/git-svn.perl
index 72f73ea623..02786f1a6a 100755
--- a/git-svn.perl
+++ b/git-svn.perl
@@ -595,8 +595,9 @@ sub cmd_multi_init {
 			command_noisy('config', 'svn.trunk', $trunk_url);
 		}
 	}
-	complete_url_ls_init($url, $_branches, '--branches/-b', $_prefix);
-	complete_url_ls_init($url, $_tags, '--tags/-t', $_prefix . 'tags/');
+	my $ra = $url ? Git::SVN::Ra->new($url) : undef;
+	complete_url_ls_init($ra, $_branches, '--branches/-b', $_prefix);
+	complete_url_ls_init($ra, $_tags, '--tags/-t', $_prefix . 'tags/');
 }
 
 sub multi_fetch {
@@ -890,29 +891,38 @@ sub complete_svn_url {
 }
 
 sub complete_url_ls_init {
-	my ($url, $path, $switch, $pfx) = @_;
+	my ($ra, $path, $switch, $pfx) = @_;
 	unless ($path) {
 		print STDERR "W: $switch not specified\n";
 		return;
 	}
-	my $full_url = complete_svn_url($url, $path);
-	my @ls = libsvn_ls_fullurl($full_url);
-	foreach my $u (map { "$full_url/$_" } (grep m!/$!, @ls)) {
-		$u =~ s#/+$##;
-		if ($u !~ m!\Q$full_url\E/(.+)$!) {
-			print STDERR "W: Unrecognized URL: $u\n";
-			die "This should never happen\n";
+	$path =~ s#/+$##;
+	if ($path =~ m#^[a-z\+]+://#) {
+		$ra = Git::SVN::Ra->new($path);
+		$path = '';
+	} else {
+		$path =~ s#^/+##;
+		unless ($ra) {
+			fatal("E: '$path' is not a complete URL ",
+			      "and a separate URL is not specified\n");
 		}
-		# don't try to init already existing refs
-		my $id = $pfx.$1;
+	}
+	my $r = defined $_revision ? $_revision : $ra->get_latest_revnum;
+	my ($dirent, undef, undef) = $ra->get_dir($path, $r);
+	my $url = $ra->{url} . (length $path ? "/$path" : '');
+	foreach my $d (sort keys %$dirent) {
+		next if ($dirent->{$d}->kind != $SVN::Node::dir);
+		my $u =  "$url/$d";
+		my $id = "$pfx$d";
 		my $gs = eval { Git::SVN->new($id) };
+		# don't try to init already existing refs
 		unless ($gs) {
 			print "init $u => $id\n";
 			Git::SVN->init($id, $u);
 		}
 	}
 	my ($n) = ($switch =~ /^--(\w+)/);
-	command_noisy('config', "svn.$n", $full_url);
+	command_noisy('config', "svn.$n", $url);
 }
 
 sub common_prefix {
@@ -2851,20 +2861,6 @@ sub libsvn_commit_cb {
 	}
 }
 
-sub libsvn_ls_fullurl {
-	my $fullurl = shift;
-	my $ra = Git::SVN::Ra->new($fullurl);
-	my @ret;
-	my $r = defined $_revision ? $_revision : $ra->get_latest_revnum;
-	my ($dirent, undef, undef) = $ra->get_dir('', $r);
-	foreach my $d (sort keys %$dirent) {
-		if ($dirent->{$d}->kind == $SVN::Node::dir) {
-			push @ret, "$d/"; # add '/' for compat with cli svn
-		}
-	}
-	return @ret;
-}
-
 sub libsvn_skip_unknown_revs {
 	my $err = shift;
 	my $errno = $err->apr_err();
diff --git a/t/t9103-git-svn-graft-branches.sh b/t/t9103-git-svn-graft-branches.sh
index 183ae3b1c2..8d946d2aa5 100755
--- a/t/t9103-git-svn-graft-branches.sh
+++ b/t/t9103-git-svn-graft-branches.sh
@@ -26,6 +26,12 @@ test_expect_success 'initialize repo' "
 	git-svn multi-fetch
 	"
 
+test_expect_success 'multi-init set .git/config correctly' "
+	test '$svnrepo/trunk' = '`git repo-config --get svn.trunk`' &&
+	test '$svnrepo/branches' = '`git repo-config --get svn.branches`' &&
+	test '$svnrepo/tags' = '`git repo-config --get svn.tags`'
+	"
+
 r1=`git-rev-list remotes/trunk | tail -n1`
 r2=`git-rev-list remotes/tags/a | tail -n1`
 r3=`git-rev-list remotes/a | tail -n1`