Skip to content

Commit d2fd119

Browse files
schwernEric Wong
authored andcommitted
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>
1 parent 8266fc8 commit d2fd119

File tree

5 files changed

+75
-23
lines changed

5 files changed

+75
-23
lines changed

git-svn.perl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
canonicalize_path
3636
canonicalize_url
3737
join_paths
38+
add_path_to_url
3839
);
3940

4041
use Git qw(
@@ -1436,7 +1437,7 @@ sub cmd_info {
14361437
# canonicalize_path() will return "" to make libsvn 1.5.x happy,
14371438
$path = "." if $path eq "";
14381439

1439-
my $full_url = canonicalize_url( $url . ($fullpath eq "" ? "" : "/$fullpath") );
1440+
my $full_url = canonicalize_url( add_path_to_url( $url, $fullpath ) );
14401441

14411442
if ($_url) {
14421443
print "$full_url\n";

perl/Git/SVN.pm

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use Git::SVN::Utils qw(
2929
join_paths
3030
canonicalize_path
3131
canonicalize_url
32+
add_path_to_url
3233
);
3334

3435
my $can_use_yaml;
@@ -564,8 +565,7 @@ sub _set_svm_vars {
564565
# username is of no interest
565566
$src =~ s{(^[a-z\+]*://)[^/@]*@}{$1};
566567

567-
my $replace = $ra->url;
568-
$replace .= "/$path" if length $path;
568+
my $replace = add_path_to_url($ra->url, $path);
569569

570570
my $section = "svn-remote.$self->{repo_id}";
571571
tmp_config("$section.svm-source", $src);
@@ -582,7 +582,7 @@ sub _set_svm_vars {
582582
my $path = $self->path;
583583
my %tried;
584584
while (length $path) {
585-
my $try = $self->url . "/$path";
585+
my $try = add_path_to_url($self->url, $path);
586586
unless ($tried{$try}) {
587587
return $ra if $self->read_svm_props($ra, $path, $r);
588588
$tried{$try} = 1;
@@ -591,7 +591,7 @@ sub _set_svm_vars {
591591
}
592592
die "Path: '$path' should be ''\n" if $path ne '';
593593
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;
595595

596596
if ($ra->{repos_root} eq $self->url) {
597597
die @err, (map { " $_\n" } keys %tried), "\n";
@@ -603,7 +603,7 @@ sub _set_svm_vars {
603603
$path = $ra->{svn_path};
604604
$ra = Git::SVN::Ra->new($ra->{repos_root});
605605
while (length $path) {
606-
my $try = $ra->url ."/$path";
606+
my $try = add_path_to_url($ra->url, $path);
607607
unless ($tried{$try}) {
608608
$ok = $self->read_svm_props($ra, $path, $r);
609609
last if $ok;
@@ -613,7 +613,7 @@ sub _set_svm_vars {
613613
}
614614
die "Path: '$path' should be ''\n" if $path ne '';
615615
$ok ||= $self->read_svm_props($ra, $path, $r);
616-
$tried{$ra->url ."/$path"} = 1;
616+
$tried{ add_path_to_url($ra->url, $path) } = 1;
617617
if (!$ok) {
618618
die @err, (map { " $_\n" } keys %tried), "\n";
619619
}
@@ -933,20 +933,19 @@ sub rewrite_uuid {
933933

934934
sub metadata_url {
935935
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 );
938938
}
939939

940940
sub full_url {
941941
my ($self) = @_;
942-
$self->url . (length $self->path ? '/' . $self->path : '');
942+
return add_path_to_url( $self->url, $self->path );
943943
}
944944

945945
sub full_pushurl {
946946
my ($self) = @_;
947947
if ($self->{pushurl}) {
948-
return $self->{pushurl} . (length $self->path ? '/' .
949-
$self->path : '');
948+
return add_path_to_url( $self->{pushurl}, $self->path );
950949
} else {
951950
return $self->full_url;
952951
}
@@ -1114,7 +1113,7 @@ sub find_parent_branch {
11141113
my $r = $i->{copyfrom_rev};
11151114
my $repos_root = $self->ra->{repos_root};
11161115
my $url = $self->ra->url;
1117-
my $new_url = $url . $branch_from;
1116+
my $new_url = add_path_to_url( $url, $branch_from );
11181117
print STDERR "Found possible branch point: ",
11191118
"$new_url => ", $self->full_url, ", $r\n"
11201119
unless $::_q > 1;
@@ -1443,12 +1442,11 @@ sub find_extra_svk_parents {
14431442
for my $ticket ( @tickets ) {
14441443
my ($uuid, $path, $rev) = split /:/, $ticket;
14451444
if ( $uuid eq $self->ra_uuid ) {
1446-
my $url = $self->url;
1447-
my $repos_root = $url;
1445+
my $repos_root = $self->url;
14481446
my $branch_from = $path;
14491447
$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,
14521450
$branch_from,
14531451
$rev,
14541452
$self->{ref_id});
@@ -1871,8 +1869,7 @@ sub make_log_entry {
18711869
$email ||= "$author\@$uuid";
18721870
$commit_email ||= "$author\@$uuid";
18731871
} 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 );
18761873
remove_username($full_url);
18771874
my $uuid = $self->svnsync->{uuid};
18781875
$log_entry{metadata} = "$full_url\@$rev $uuid";

perl/Git/SVN/Ra.pm

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use warnings;
55
use SVN::Client;
66
use Git::SVN::Utils qw(
77
canonicalize_url
8+
add_path_to_url
89
);
910

1011
use SVN::Ra;
@@ -287,9 +288,8 @@ sub gs_do_switch {
287288
my $path = $gs->path;
288289
my $pool = SVN::Pool->new;
289290

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 );
293293
my ($ra, $reparented);
294294

295295
if ($old_url =~ m#^svn(\+ssh)?://# ||
@@ -555,7 +555,7 @@ sub minimize_url {
555555
my @components = split(m!/!, $self->{svn_path});
556556
my $c = '';
557557
do {
558-
$url .= "/$c" if length $c;
558+
$url = add_path_to_url($url, $c);
559559
eval {
560560
my $ra = (ref $self)->new($url);
561561
my $latest = $ra->get_latest_revnum;

perl/Git/SVN/Utils.pm

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ our @EXPORT_OK = qw(
1313
canonicalize_path
1414
canonicalize_url
1515
join_paths
16+
add_path_to_url
1617
);
1718

1819

@@ -203,4 +204,30 @@ sub join_paths {
203204
return $new_path .= "/$last_path";
204205
}
205206

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+
206233
1;

t/Git-SVN/Utils/add_path_to_url.t

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

0 commit comments

Comments
 (0)