git-svn: introduce add_path_to_url function
Remove the ad-hoc versions. This is mostly to normalize the process and ensure the URLs produced don't have double slashes or anything. Also provides a place to fix the corner case where a file path contains a percent sign. [ew: commit title] Signed-off-by: Eric Wong <normalperson@yhbt.net>
Michael G. Schwern committed
Jul 28, 2012 at 02:47 UTC
d2fd119c4fcaea266a894354b506959376140c37
5 files changed
+75
-23
git-svn.perl
+2
-1
@@ -35,6 +35,7 @@ use Git::SVN::Utils qw(
35
canonicalize_path
36
canonicalize_url
37
join_paths
38
+ add_path_to_url
39
);
40
41
use Git qw(
@@ -1436,7 +1437,7 @@ sub cmd_info {
1437
# canonicalize_path() will return "" to make libsvn 1.5.x happy,
1438
$path = "." if $path eq "";
1439
1439
- my $full_url = canonicalize_url( $url . ($fullpath eq "" ? "" : "/$fullpath") );
1440
+ my $full_url = canonicalize_url( add_path_to_url( $url, $fullpath ) );
1441
1442
if ($_url) {
1443
print "$full_url\n";
perl/Git/SVN.pm
+15
-18
@@ -29,6 +29,7 @@ use Git::SVN::Utils qw(
29
join_paths
30
canonicalize_path
31
canonicalize_url
32
+ add_path_to_url
33
);
34
35
my $can_use_yaml;
@@ -564,8 +565,7 @@ sub _set_svm_vars {
565
# username is of no interest
566
$src =~ s{(^[a-z\+]*://)[^/@]*@}{$1};
567
567
- my $replace = $ra->url;
568
- $replace .= "/$path" if length $path;
568
+ my $replace = add_path_to_url($ra->url, $path);
569
570
my $section = "svn-remote.$self->{repo_id}";
571
tmp_config("$section.svm-source", $src);
@@ -582,7 +582,7 @@ sub _set_svm_vars {
582
my $path = $self->path;
583
my %tried;
584
while (length $path) {
585
- my $try = $self->url . "/$path";
585
+ my $try = add_path_to_url($self->url, $path);
586
unless ($tried{$try}) {
587
return $ra if $self->read_svm_props($ra, $path, $r);
588
$tried{$try} = 1;
@@ -591,7 +591,7 @@ sub _set_svm_vars {
591
}
592
die "Path: '$path' should be ''\n" if $path ne '';
593
return $ra if $self->read_svm_props($ra, $path, $r);
594
- $tried{$self->url."/$path"} = 1;
594
+ $tried{ add_path_to_url($self->url, $path) } = 1;
595
596
if ($ra->{repos_root} eq $self->url) {
597
die @err, (map { " $_\n" } keys %tried), "\n";
@@ -603,7 +603,7 @@ sub _set_svm_vars {
603
$path = $ra->{svn_path};
604
$ra = Git::SVN::Ra->new($ra->{repos_root});
605
while (length $path) {
606
- my $try = $ra->url ."/$path";
606
+ my $try = add_path_to_url($ra->url, $path);
607
unless ($tried{$try}) {
608
$ok = $self->read_svm_props($ra, $path, $r);
609
last if $ok;
@@ -613,7 +613,7 @@ sub _set_svm_vars {
613
}
614
die "Path: '$path' should be ''\n" if $path ne '';
615
$ok ||= $self->read_svm_props($ra, $path, $r);
616
- $tried{$ra->url ."/$path"} = 1;
616
+ $tried{ add_path_to_url($ra->url, $path) } = 1;
617
if (!$ok) {
618
die @err, (map { " $_\n" } keys %tried), "\n";
619
}
@@ -933,20 +933,19 @@ sub rewrite_uuid {
933
934
sub metadata_url {
935
my ($self) = @_;
936
- ($self->rewrite_root || $self->url) .
937
- (length $self->path ? '/' . $self->path : '');
936
+ my $url = $self->rewrite_root || $self->url;
937
+ return add_path_to_url( $url, $self->path );
938
}
939
940
sub full_url {
941
my ($self) = @_;
942
- $self->url . (length $self->path ? '/' . $self->path : '');
942
+ return add_path_to_url( $self->url, $self->path );
943
}
944
945
sub full_pushurl {
946
my ($self) = @_;
947
if ($self->{pushurl}) {
948
- return $self->{pushurl} . (length $self->path ? '/' .
949
- $self->path : '');
948
+ return add_path_to_url( $self->{pushurl}, $self->path );
949
} else {
950
return $self->full_url;
951
}
@@ -1114,7 +1113,7 @@ sub find_parent_branch {
1113
my $r = $i->{copyfrom_rev};
1114
my $repos_root = $self->ra->{repos_root};
1115
my $url = $self->ra->url;
1117
- my $new_url = $url . $branch_from;
1116
+ my $new_url = add_path_to_url( $url, $branch_from );
1117
print STDERR "Found possible branch point: ",
1118
"$new_url => ", $self->full_url, ", $r\n"
1119
unless $::_q > 1;
@@ -1443,12 +1442,11 @@ sub find_extra_svk_parents {
1442
for my $ticket ( @tickets ) {
1443
my ($uuid, $path, $rev) = split /:/, $ticket;
1444
if ( $uuid eq $self->ra_uuid ) {
1446
- my $url = $self->url;
1447
- my $repos_root = $url;
1445
+ my $repos_root = $self->url;
1446
my $branch_from = $path;
1447
$branch_from =~ s{^/}{};
1450
- my $gs = $self->other_gs($repos_root."/".$branch_from,
1451
- $url,
1448
+ my $gs = $self->other_gs(add_path_to_url( $repos_root, $branch_from ),
1449
+ $repos_root,
1450
$branch_from,
1451
$rev,
1452
$self->{ref_id});
@@ -1871,8 +1869,7 @@ sub make_log_entry {
1869
$email ||= "$author\@$uuid";
1870
$commit_email ||= "$author\@$uuid";
1871
} elsif ($self->use_svnsync_props) {
1874
- my $full_url = $self->svnsync->{url};
1875
- $full_url .= "/".$self->path if length $self->path;
1872
+ my $full_url = add_path_to_url( $self->svnsync->{url}, $self->path );
1873
remove_username($full_url);
1874
my $uuid = $self->svnsync->{uuid};
1875
$log_entry{metadata} = "$full_url\@$rev $uuid";
perl/Git/SVN/Ra.pm
+4
-4
@@ -5,6 +5,7 @@ use warnings;
5
use SVN::Client;
6
use Git::SVN::Utils qw(
7
canonicalize_url
8
+ add_path_to_url
9
);
10
11
use SVN::Ra;
@@ -287,9 +288,8 @@ sub gs_do_switch {
288
my $path = $gs->path;
289
my $pool = SVN::Pool->new;
290
290
- my $full_url = $self->url;
291
- my $old_url = $full_url;
292
- $full_url .= '/' . $path if length $path;
291
+ my $old_url = $self->url;
292
+ my $full_url = add_path_to_url( $self->url, $path );
293
my ($ra, $reparented);
294
295
if ($old_url =~ m#^svn(\+ssh)?://# ||
@@ -555,7 +555,7 @@ sub minimize_url {
555
my @components = split(m!/!, $self->{svn_path});
556
my $c = '';
557
do {
558
- $url .= "/$c" if length $c;
558
+ $url = add_path_to_url($url, $c);
559
eval {
560
my $ra = (ref $self)->new($url);
561
my $latest = $ra->get_latest_revnum;
perl/Git/SVN/Utils.pm
+27
@@ -13,6 +13,7 @@ our @EXPORT_OK = qw(
13
canonicalize_path
14
canonicalize_url
15
join_paths
16
+ add_path_to_url
17
);
18
19
@@ -203,4 +204,30 @@ sub join_paths {
204
return $new_path .= "/$last_path";
205
}
206
207
+
208
+=head3 add_path_to_url
209
+
210
+ my $new_url = add_path_to_url($url, $path);
211
+
212
+Appends $path onto the $url. If $path is empty, $url is returned unchanged.
213
+
214
+=cut
215
+
216
+sub add_path_to_url {
217
+ my($url, $path) = @_;
218
+
219
+ return $url if !defined $path or !length $path;
220
+
221
+ # Strip trailing and leading slashes so we don't
222
+ # wind up with http://x.com///path
223
+ $url =~ s{/+$}{};
224
+ $path =~ s{^/+}{};
225
+
226
+ # If a path has a % in it, URI escape it so it's not
227
+ # mistaken for a URI escape later.
228
+ $path =~ s{%}{%25}g;
229
+
230
+ return join '/', $url, $path;
231
+}
232
+
233
1;
t/Git-SVN/Utils/add_path_to_url.t
new
+27
@@ -0,0 +1,27 @@
1
+#!/usr/bin/env perl
2
+
3
+use strict;
4
+use warnings;
5
+
6
+use Test::More 'no_plan';
7
+
8
+use Git::SVN::Utils qw(
9
+ add_path_to_url
10
+);
11
+
12
+# A reference cannot be a hash key, so we use an array.
13
+my @tests = (
14
+ ["http://x.com", "bar"] => 'http://x.com/bar',
15
+ ["http://x.com", ""] => 'http://x.com',
16
+ ["http://x.com/foo/", undef] => 'http://x.com/foo/',
17
+ ["http://x.com/foo/", "/bar/baz/"] => 'http://x.com/foo/bar/baz/',
18
+ ["http://x.com", 'per%cent'] => 'http://x.com/per%25cent',
19
+);
20
+
21
+while(@tests) {
22
+ my($have, $want) = splice @tests, 0, 2;
23
+
24
+ my $args = join ", ", map { qq['$_'] } map { defined($_) ? $_ : 'undef' } @$have;
25
+ my $name = "add_path_to_url($args) eq $want";
26
+ is add_path_to_url(@$have), $want, $name;
27
+}