@matheusbsbs / matheus-bagliano / commits / e6434f8760
Fork 0

git-svn: 'init' attempts to connect to the repository root if possible

This allows connections to be used more efficiently and not require users to run 'git-svn migrate --minimize' for new repositories. Signed-off-by: Eric Wong <normalperson@yhbt.net>

Eric Wong committed Jan 23, 2007 at 16:29 UTC e6434f876097f196acbd9a806637d0f6076752fd
2 files changed +89 -21
git-svn.perl
+76 -20
@@ -748,35 +748,78 @@ sub sanitize_remote_name {
748 $name;
749 }
750
751 -sub init {
752 - my ($class, $url, $path, $repo_id, $ref_id) = @_;
753 - my $self = _new($class, $repo_id, $ref_id, $path);
754 - if (defined $url) {
755 - $url =~ s!/+$!!; # strip trailing slash
751 +sub find_existing_remote {
752 + my ($url, $remotes) = @_;
753 + my $existing;
754 + foreach my $repo_id (keys %$remotes) {
755 + my $u = $remotes->{$repo_id}->{url} or next;
756 + next if $u ne $url;
757 + $existing = $repo_id;
758 + last;
759 + }
760 + $existing;
761 +}
762
763 +sub init_remote_config {
764 + my ($self, $url) = @_;
765 + $url =~ s!/+$!!; # strip trailing slash
766 + my $r = read_all_remotes();
767 + my $existing = find_existing_remote($url, $r);
768 + if ($existing) {
769 + print STDERR "Using existing ",
770 + "[svn-remote \"$existing\"]\n";
771 + $self->{repo_id} = $existing;
772 + } else {
773 + my $min_url = Git::SVN::Ra->new($url)->minimize_url;
774 + $existing = find_existing_remote($min_url, $r);
775 + if ($existing) {
776 + print STDERR "Using existing ",
777 + "[svn-remote \"$existing\"]\n";
778 + $self->{repo_id} = $existing;
779 + }
780 + if ($min_url ne $url) {
781 + print STDERR "Using higher level of URL: ",
782 + "$url => $min_url\n";
783 + my $old_path = $self->{path};
784 + $self->{path} = $url;
785 + $self->{path} =~ s!^\Q$min_url\E/*!!;
786 + if (length $old_path) {
787 + $self->{path} .= "/$old_path";
788 + }
789 + $url = $min_url;
790 + }
791 + }
792 + my $orig_url;
793 + if (!$existing) {
794 # verify that we aren't overwriting anything:
758 - my $orig_url = eval {
795 + $orig_url = eval {
796 command_oneline('config', '--get',
760 - "svn-remote.$repo_id.url")
797 + "svn-remote.$self->{repo_id}.url")
798 };
799 if ($orig_url && ($orig_url ne $url)) {
763 - die "svn-remote.$repo_id.url already set: ",
800 + die "svn-remote.$self->{repo_id}.url already set: ",
801 "$orig_url\nwanted to set to: $url\n";
802 }
766 - my ($xrepo_id, $xpath) = find_ref($self->refname);
767 - if (defined $xpath) {
768 - die "svn-remote.$xrepo_id.fetch already set to track ",
769 - "$xpath:refs/remotes/", $self->refname, "\n";
770 - }
771 - if (!$orig_url) {
772 - command_noisy('config',
773 - "svn-remote.$repo_id.url", $url);
774 - }
775 - command_noisy('config', '--add',
776 - "svn-remote.$repo_id.fetch",
777 - "$path:".$self->refname);
803 }
804 + my ($xrepo_id, $xpath) = find_ref($self->refname);
805 + if (defined $xpath) {
806 + die "svn-remote.$xrepo_id.fetch already set to track ",
807 + "$xpath:refs/remotes/", $self->refname, "\n";
808 + }
809 + command_noisy('config',
810 + "svn-remote.$self->{repo_id}.url", $url);
811 + command_noisy('config', '--add',
812 + "svn-remote.$self->{repo_id}.fetch",
813 + "$self->{path}:".$self->refname);
814 $self->{url} = $url;
815 +}
816 +
817 +sub init {
818 + my ($class, $url, $path, $repo_id, $ref_id) = @_;
819 + my $self = _new($class, $repo_id, $ref_id, $path);
820 + if (defined $url) {
821 + $self->init_remote_config($url);
822 + }
823 $self;
824 }
825
@@ -2208,6 +2251,19 @@ sub gs_do_switch {
2251 $editor->{git_commit_ok};
2252 }
2253
2254 +sub minimize_url {
2255 + my ($self) = @_;
2256 + return $self->{url} if ($self->{url} eq $self->{repos_root});
2257 + my $url = $self->{repos_root};
2258 + my @components = split(m!/!, $self->{svn_path});
2259 + my $c = '';
2260 + do {
2261 + $url .= "/$c" if length $c;
2262 + eval { (ref $self)->new($url)->get_latest_revnum };
2263 + } while ($@ && ($c = shift @components));
2264 + $url;
2265 +}
2266 +
2267 sub can_do_switch {
2268 my $self = shift;
2269 unless (defined $can_do_switch) {
t/t9100-git-svn-basic.sh
+13 -1
@@ -220,10 +220,22 @@ test_expect_failure 'exit if remote refs are ambigious' "
220 bar:refs/remotes/git-svn &&
221 git-svn migrate
222 "
223 +
224 test_expect_failure 'exit if init-ing a would clobber a URL' "
225 + svnadmin create ${PWD}/svnrepo2 &&
226 + svn mkdir -m 'mkdir bar' ${svnrepo}2/bar &&
227 git-repo-config --unset svn-remote.git-svn.fetch \
228 '^bar:refs/remotes/git-svn$' &&
226 - git-svn init $svnrepo/bar
229 + git-svn init ${svnrepo}2/bar
230 + "
231 +
232 +test_expect_success \
233 + 'init allows us to connect to another directory in the same repo' "
234 + git-svn init -i bar $svnrepo/bar &&
235 + git repo-config --get svn-remote.git-svn.fetch \
236 + '^bar:refs/remotes/bar$' &&
237 + git repo-config --get svn-remote.git-svn.fetch \
238 + '^:refs/remotes/git-svn$'
239 "
240
241 test_done