git-svn: enable --minimize to simplify the config and connections
--minimize will update the git-svn configuration to attempt to connect to the repository root (instead of directly to the path(s) we are tracking) in order to allow more efficient reuse of connections (for multi-fetch and follow-parent). Signed-off-by: Eric Wong <normalperson@yhbt.net>
Eric Wong committed
Jan 21, 2007 at 04:27 UTC
47e39c55c91993b94824b7a317ebeb965aaeb45a
2 files changed
+129
-6
git-svn.perl
+104
-6
@@ -119,8 +119,6 @@ my %cmd = (
119
\%fc_opts ],
120
'migrate' => [ sub { },
121
# no-op, we automatically run this anyways,
122
- # we may add a flag to automatically optimize the
123
- # configuration to avoid reconnects in the future
122
'Migrate configuration/metadata/layout from
123
previous versions of git-svn',
124
\%remote_opts ],
@@ -158,6 +156,8 @@ my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
156
read_repo_config(\%opts);
157
my $rv = GetOptions(%opts, 'help|H|h' => \$_help,
158
'version|V' => \$_version,
159
+ 'minimize-connections' =>
160
+ \$Git::SVN::Migration::_minimize,
161
'id|i=s' => \$Git::SVN::default_repo_id);
162
exit 1 if (!$rv && $cmd ne 'log');
163
@@ -702,10 +702,22 @@ BEGIN {
702
svn:entry:committed-date/;
703
}
704
705
-# we allow dashes, unlike remotes2config.sh
705
+sub read_all_remotes {
706
+ my $r = {};
707
+ foreach (grep { s/^svn-remote\.// } command(qw/repo-config -l/)) {
708
+ if (m!^(.+)\.fetch=\s*(.*)\s*:\s*refs/remotes/(.+)\s*$!) {
709
+ $r->{$1}->{fetch}->{$2} = $3;
710
+ } elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
711
+ $r->{$1}->{url} = $2;
712
+ }
713
+ }
714
+ $r;
715
+}
716
+
717
+# we allow more chars than remotes2config.sh...
718
sub sanitize_remote_name {
719
my ($name) = @_;
708
- $name =~ tr/A-Za-z0-9-/./c;
720
+ $name =~ tr{A-Za-z0-9:,/+-}{.}c;
721
$name;
722
}
723
@@ -2467,7 +2479,8 @@ use strict;
2479
use warnings;
2480
use Carp qw/croak/;
2481
use File::Path qw/mkpath/;
2470
-use File::Basename qw/dirname/;
2482
+use File::Basename qw/dirname basename/;
2483
+use vars qw/$_minimize/;
2484
2485
sub migrate_from_v0 {
2486
my $git_dir = $ENV{GIT_DIR};
@@ -2577,16 +2590,101 @@ sub migrate_from_v2 {
2590
my $migrated = 0;
2591
2592
foreach my $ref_id (sort keys %l_map) {
2580
- Git::SVN->init($l_map{$ref_id}, $ref_id);
2593
+ Git::SVN->init($l_map{$ref_id}, '', $ref_id, $ref_id);
2594
$migrated++;
2595
}
2596
$migrated;
2597
}
2598
2599
+sub minimize_connections {
2600
+ my $r = Git::SVN::read_all_remotes();
2601
+ my $new_urls = {};
2602
+ my $root_repos = {};
2603
+ foreach my $repo_id (keys %$r) {
2604
+ my $url = $r->{$repo_id}->{url} or next;
2605
+ my $fetch = $r->{$repo_id}->{fetch} or next;
2606
+ my $ra = Git::SVN::Ra->new($url);
2607
+
2608
+ # skip existing cases where we already connect to the root
2609
+ if (($ra->{url} eq $ra->{repos_root}) ||
2610
+ (Git::SVN::sanitize_remote_name($ra->{repos_root}) eq
2611
+ $repo_id)) {
2612
+ $root_repos->{$ra->{url}} = $repo_id;
2613
+ next;
2614
+ }
2615
+
2616
+ my $root_ra = Git::SVN::Ra->new($ra->{repos_root});
2617
+ my $root_path = $ra->{url};
2618
+ $root_path =~ s#^\Q$ra->{repos_root}\E/*##;
2619
+ foreach my $path (keys %$fetch) {
2620
+ my $ref_id = $fetch->{$path};
2621
+ my $gs = Git::SVN->new($ref_id, $repo_id, $path);
2622
+
2623
+ # make sure we can read when connecting to
2624
+ # a higher level of a repository
2625
+ my ($last_rev, undef) = $gs->last_rev_commit;
2626
+ if (!defined $last_rev) {
2627
+ $last_rev = eval {
2628
+ $root_ra->get_latest_revnum;
2629
+ };
2630
+ next if $@;
2631
+ }
2632
+ my $new = $root_path;
2633
+ $new .= length $path ? "/$path" : '';
2634
+ eval {
2635
+ $root_ra->get_log([$new], $last_rev, $last_rev,
2636
+ 0, 0, 1, sub { });
2637
+ };
2638
+ next if $@;
2639
+ $new_urls->{$ra->{repos_root}}->{$new} =
2640
+ { ref_id => $ref_id,
2641
+ old_repo_id => $repo_id,
2642
+ old_path => $path };
2643
+ }
2644
+ }
2645
+
2646
+ my @emptied;
2647
+ foreach my $url (keys %$new_urls) {
2648
+ # see if we can re-use an existing [svn-remote "repo_id"]
2649
+ # instead of creating a(n ugly) new section:
2650
+ my $repo_id = $root_repos->{$url} ||
2651
+ Git::SVN::sanitize_remote_name($url);
2652
+
2653
+ my $fetch = $new_urls->{$url};
2654
+ foreach my $path (keys %$fetch) {
2655
+ my $x = $fetch->{$path};
2656
+ Git::SVN->init($url, $path, $repo_id, $x->{ref_id});
2657
+ my $pfx = "svn-remote.$x->{old_repo_id}";
2658
+
2659
+ my $old_fetch = quotemeta("$x->{old_path}:".
2660
+ "refs/remotes/$x->{ref_id}");
2661
+ command_noisy(qw/repo-config --unset/,
2662
+ "$pfx.fetch", '^'. $old_fetch . '$');
2663
+ delete $r->{$x->{old_repo_id}}->
2664
+ {fetch}->{$x->{old_path}};
2665
+ if (!keys %{$r->{$x->{old_repo_id}}->{fetch}}) {
2666
+ command_noisy(qw/repo-config --unset/,
2667
+ "$pfx.url");
2668
+ push @emptied, $x->{old_repo_id}
2669
+ }
2670
+ }
2671
+ }
2672
+ if (@emptied) {
2673
+ my $file = $ENV{GIT_CONFIG} || $ENV{GIT_CONFIG_LOCAL} ||
2674
+ "$ENV{GIT_DIR}/config";
2675
+ print STDERR <<EOF;
2676
+The following [svn-remote] sections in your config file ($file) are empty
2677
+and can be safely removed:
2678
+EOF
2679
+ print STDERR "[svn-remote \"$_\"]\n" foreach @emptied;
2680
+ }
2681
+}
2682
+
2683
sub migration_check {
2684
migrate_from_v0();
2685
migrate_from_v1();
2686
migrate_from_v2();
2687
+ minimize_connections() if $_minimize;
2688
}
2689
2690
__END__
t/t9107-git-svn-migrate.sh
+25
@@ -46,6 +46,7 @@ test_expect_success 'initialize a multi-repository repo' "
46
grep '^tags/0\.3:refs/remotes/tags/0\.3$' fetch.out
47
"
48
49
+# refs should all be different, but the trees should all be the same:
50
test_expect_success 'multi-fetch works on partial urls + paths' "
51
git-svn multi-fetch &&
52
for i in trunk a b tags/0.1 tags/0.2 tags/0.3; do
@@ -59,5 +60,29 @@ test_expect_success 'multi-fetch works on partial urls + paths' "
60
refs/remotes/\$j\`\" ||exit 1; done; done
61
"
62
63
+test_expect_success 'migrate --minimize on old multi-inited layout' "
64
+ git repo-config --unset-all svn-remote.git-svn.fetch &&
65
+ git repo-config --unset-all svn-remote.git-svn.url &&
66
+ rm -rf $GIT_DIR/svn &&
67
+ for i in \`cat fetch.out\`; do
68
+ path=\`expr \$i : '\\([^:]*\\):.*$'\`
69
+ ref=\`expr \$i : '[^:]*:refs/remotes/\\(.*\\)$'\`
70
+ if test -z \"\$ref\"; then continue; fi
71
+ if test -n \"\$path\"; then path=\"/\$path\"; fi
72
+ ( mkdir -p $GIT_DIR/svn/\$ref/info/ &&
73
+ echo $svnrepo\$path > $GIT_DIR/svn/\$ref/info/url ) || exit 1;
74
+ done &&
75
+ git-svn migrate --minimize &&
76
+ test -z \"\`git-repo-config -l |grep -v '^svn-remote\.git-svn\.'\`\" &&
77
+ git-repo-config --get-all svn-remote.git-svn.fetch > fetch.out &&
78
+ grep '^trunk:refs/remotes/trunk$' fetch.out &&
79
+ grep '^branches/a:refs/remotes/a$' fetch.out &&
80
+ grep '^branches/b:refs/remotes/b$' fetch.out &&
81
+ grep '^tags/0\.1:refs/remotes/tags/0\.1$' fetch.out &&
82
+ grep '^tags/0\.2:refs/remotes/tags/0\.2$' fetch.out &&
83
+ grep '^tags/0\.3:refs/remotes/tags/0\.3$' fetch.out
84
+ grep '^:refs/remotes/git-svn' fetch.out
85
+ "
86
+
87
test_done
88