git-svn: prepare multi-init for wildcard support
Update the tests since we no longer write so many things to the config. Signed-off-by: Eric Wong <normalperson@yhbt.net>
Eric Wong committed
Feb 3, 2007 at 13:29 UTC
4bb9ed0466e9ca6b72b3d9c454a743aea862b1f4
2 files changed
+131
-13
git-svn.perl
+117
-7
@@ -367,9 +367,10 @@ sub cmd_multi_init {
367
sub cmd_multi_fetch {
368
my $remotes = Git::SVN::read_all_remotes();
369
foreach my $repo_id (sort keys %$remotes) {
370
- my $url = $remotes->{$repo_id}->{url} or next;
371
- my $fetch = $remotes->{$repo_id}->{fetch} or next;
372
- Git::SVN::fetch_all($repo_id, $url, $fetch);
370
+ if ($remotes->{$repo_id}->{url} &&
371
+ $remotes->{$repo_id}->{fetch}) {
372
+ Git::SVN::fetch_all($repo_id, $remotes);
373
+ }
374
}
375
}
376
@@ -479,9 +480,12 @@ sub complete_url_ls_init {
480
# don't try to init already existing refs
481
unless ($gs) {
482
print "init $url/$path => $ref\n";
482
- $gs = Git::SVN->init($url, $path, undef, $ref);
483
+ $gs = Git::SVN->init($url, $path, undef, $ref, 1);
484
+ }
485
+ if ($gs) {
486
+ $remote_id = $gs->{repo_id};
487
+ last;
488
}
484
- $remote_id ||= $gs->{repo_id} if $gs;
489
}
490
if (defined $remote_id) {
491
$remote_path = "$ra->{svn_path}/$repo_path/*";
@@ -489,7 +493,7 @@ sub complete_url_ls_init {
493
$remote_path =~ s#^/##g;
494
my ($n) = ($switch =~ /^--(\w+)/);
495
command_noisy('config', "svn-remote.$remote_id.$n",
492
- $remote_path);
496
+ "$remote_path:refs/remotes/$pfx*");
497
}
498
}
499
@@ -660,9 +664,48 @@ BEGIN {
664
my %LOCKFILES;
665
END { unlink keys %LOCKFILES if %LOCKFILES }
666
667
+sub resolve_local_globs {
668
+ my ($url, $fetch, $glob_spec) = @_;
669
+ return unless defined $glob_spec;
670
+ my $ref = $glob_spec->{ref};
671
+ my $path = $glob_spec->{path};
672
+ foreach (command(qw#for-each-ref --format=%(refname) refs/remotes#)) {
673
+ next unless m#^refs/remotes/$ref->{regex}$#;
674
+ my $p = $1;
675
+ my $pathname = $path->full_path($p);
676
+ my $refname = $ref->full_path($p);
677
+ if (my $existing = $fetch->{$pathname}) {
678
+ if ($existing ne $refname) {
679
+ die "Refspec conflict:\n",
680
+ "existing: refs/remotes/$existing\n",
681
+ " globbed: refs/remotes/$refname\n";
682
+ }
683
+ my $u = (::cmt_metadata("refs/remotes/$refname"))[0];
684
+ $u =~ s!^\Q$url\E/?!! or die
685
+ "refs/remotes/$refname: '$url' not found in '$u'\n";
686
+ if ($pathname ne $u) {
687
+ warn "W: Refspec glob conflict ",
688
+ "(ref: refs/remotes/$refname):\n",
689
+ "expected path: $pathname\n",
690
+ " real path: $u\n",
691
+ "Continuing ahead with $u\n";
692
+ next;
693
+ }
694
+ } else {
695
+ warn "Globbed ($path->{glob}:$ref->{glob}): ",
696
+ "$pathname == $refname\n";
697
+ $fetch->{$pathname} = $refname;
698
+ }
699
+ }
700
+}
701
+
702
sub fetch_all {
664
- my ($repo_id, $url, $fetch) = @_;
703
+ my ($repo_id, $remotes) = @_;
704
+ my $fetch = $remotes->{$repo_id}->{fetch};
705
+ my $url = $remotes->{$repo_id}->{url};
706
my @gs;
707
+ resolve_local_globs($url, $fetch, $remotes->{$repo_id}->{branches});
708
+ resolve_local_globs($url, $fetch, $remotes->{$repo_id}->{tags});
709
my $ra = Git::SVN::Ra->new($url);
710
my $head = $ra->get_latest_revnum;
711
my $base = $head;
@@ -685,6 +728,16 @@ sub read_all_remotes {
728
$r->{$1}->{fetch}->{$2} = $3;
729
} elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
730
$r->{$1}->{url} = $2;
731
+ } elsif (m!^(.+)\.(branches|tags)=
732
+ (.*):refs/remotes/(.+)\s*$/!x) {
733
+ my ($p, $g) = ($3, $4);
734
+ my $rs = $r->{$1}->{$2} = {
735
+ path => Git::SVN::GlobSpec->new($p),
736
+ ref => Git::SVN::GlobSpec->new($g) };
737
+ if (length($rs->{ref}->{right}) != 0) {
738
+ die "The '*' glob character must be the last ",
739
+ "character of '$g'\n";
740
+ }
741
}
742
}
743
$r;
@@ -3072,10 +3125,67 @@ sub DESTROY {
3125
command_close_pipe($self->{gui}, $self->{ctx});
3126
}
3127
3128
+package Git::SVN::GlobSpec;
3129
+use strict;
3130
+use warnings;
3131
+
3132
+sub new {
3133
+ my ($class, $glob) = @_;
3134
+ warn "glob: $glob\n";
3135
+ my $re = $glob;
3136
+ $re =~ s!/+$!!g; # no need for trailing slashes
3137
+ my $nr = ($re =~ s!^(.*/?)\*(/?.*)$!\(\[^/\]+\)!g);
3138
+ my ($left, $right) = ($1, $2);
3139
+ if ($nr > 1) {
3140
+ warn "Only one '*' wildcard expansion ",
3141
+ "is supported (got $nr): '$glob'\n";
3142
+ } elsif ($nr == 0) {
3143
+ warn "One '*' is needed for glob: '$glob'\n";
3144
+ }
3145
+ $re = quotemeta($left) . $re . quotemeta($right);
3146
+ $left =~ s!/+$!!g;
3147
+ $right =~ s!^/+!!g;
3148
+ bless { left => $left, right => $right,
3149
+ regex => qr/$re/, glob => $glob }, $class;
3150
+}
3151
+
3152
+sub full_path {
3153
+ my ($self, $path) = @_;
3154
+ return (length $self->{left} ? "$self->{left}/" : '') .
3155
+ $path . (length $self->{right} ? "/$self->{right}" : '');
3156
+}
3157
+
3158
__END__
3159
3160
Data structures:
3161
3162
+
3163
+$remotes = { # returned by read_all_remotes()
3164
+ 'svn' => {
3165
+ # svn-remote.svn.url=https://svn.musicpd.org
3166
+ url => 'https://svn.musicpd.org',
3167
+ # svn-remote.svn.fetch=mpd/trunk:trunk
3168
+ fetch => {
3169
+ 'mpd/trunk' => 'trunk',
3170
+ },
3171
+ # svn-remote.svn.tags=mpd/tags/*:tags/*
3172
+ tags => {
3173
+ path => {
3174
+ left => 'mpd/tags',
3175
+ right => '',
3176
+ regex => qr!mpd/tags/([^/]+)$!,
3177
+ glob => 'tags/*',
3178
+ },
3179
+ ref => {
3180
+ left => 'tags',
3181
+ right => '',
3182
+ regex => qr!tags/([^/]+)$!,
3183
+ glob => 'tags/*',
3184
+ },
3185
+ }
3186
+ }
3187
+};
3188
+
3189
$log_entry hashref as returned by libsvn_log_entry()
3190
{
3191
log => 'whitespace-formatted log entry
t/t9107-git-svn-migrate.sh
+14
-6
@@ -6,7 +6,7 @@ test_description='git-svn metadata migrations from previous versions'
6
test_expect_success 'setup old-looking metadata' "
7
cp $GIT_DIR/config $GIT_DIR/config-old-git-svn &&
8
mkdir import &&
9
- cd import
9
+ cd import &&
10
for i in trunk branches/a branches/b \
11
tags/0.1 tags/0.2 tags/0.3; do
12
mkdir -p \$i && \
@@ -43,11 +43,19 @@ test_expect_success 'initialize a multi-repository repo' "
43
git-svn multi-init $svnrepo -T trunk -t tags -b branches &&
44
git-repo-config --get-all svn-remote.svn.fetch > fetch.out &&
45
grep '^trunk:refs/remotes/trunk$' fetch.out &&
46
- grep '^branches/a:refs/remotes/a$' fetch.out &&
47
- grep '^branches/b:refs/remotes/b$' fetch.out &&
48
- grep '^tags/0\.1:refs/remotes/tags/0\.1$' fetch.out &&
49
- grep '^tags/0\.2:refs/remotes/tags/0\.2$' fetch.out &&
50
- grep '^tags/0\.3:refs/remotes/tags/0\.3$' fetch.out
46
+ test -n \"\`git-config --get svn-remote.svn.branches \
47
+ '^branches/\*:refs/remotes/\*$'\`\" &&
48
+ test -n \"\`git-config --get svn-remote.svn.tags \
49
+ '^tags/\*:refs/remotes/tags/\*$'\`\" &&
50
+ git config --unset svn-remote.svn.branches \
51
+ '^branches/\*:refs/remotes/\*$' &&
52
+ git config --unset svn-remote.svn.tags \
53
+ '^tags/\*:refs/remotes/tags/\*$' &&
54
+ git-config --add svn-remote.svn.fetch 'branches/a:refs/remotes/a' &&
55
+ git-config --add svn-remote.svn.fetch 'branches/b:refs/remotes/b' &&
56
+ for i in tags/0.1 tags/0.2 tags/0.3; do
57
+ git-config --add svn-remote.svn.fetch \
58
+ \$i:refs/remotes/\$i || exit 1; done
59
"
60
61
# refs should all be different, but the trees should all be the same: