git-svn: move canonicalization to Git::SVN::Utils

So they can be used by others. I'd like to test them, but they're going to become SVN API wrappers shortly and those aren't predictable. No functional change. [ew: commit title] Signed-off-by: Eric Wong <normalperson@yhbt.net>

Michael G. Schwern committed Jul 28, 2012 at 02:38 UTC 91e6e0c56cc83e5f53d93e90726380a2d392f5f1
2 files changed +58 -27
git-svn.perl
+7 -26
@@ -29,7 +29,13 @@ use Git::SVN::Prompt;
29 use Git::SVN::Log;
30 use Git::SVN::Migration;
31
32 -use Git::SVN::Utils qw(fatal can_compress);
32 +use Git::SVN::Utils qw(
33 + fatal
34 + can_compress
35 + canonicalize_path
36 + canonicalize_url
37 +);
38 +
39 use Git qw(
40 git_cmd_try
41 command
@@ -1256,31 +1262,6 @@ sub cmd_mkdirs {
1262 $gs->mkemptydirs($_revision);
1263 }
1264
1259 -sub canonicalize_path {
1260 - my ($path) = @_;
1261 - my $dot_slash_added = 0;
1262 - if (substr($path, 0, 1) ne "/") {
1263 - $path = "./" . $path;
1264 - $dot_slash_added = 1;
1265 - }
1266 - # File::Spec->canonpath doesn't collapse x/../y into y (for a
1267 - # good reason), so let's do this manually.
1268 - $path =~ s#/+#/#g;
1269 - $path =~ s#/\.(?:/|$)#/#g;
1270 - $path =~ s#/[^/]+/\.\.##g;
1271 - $path =~ s#/$##g;
1272 - $path =~ s#^\./## if $dot_slash_added;
1273 - $path =~ s#^/##;
1274 - $path =~ s#^\.$##;
1275 - return $path;
1276 -}
1277 -
1278 -sub canonicalize_url {
1279 - my ($url) = @_;
1280 - $url =~ s#^([^:]+://[^/]*/)(.*)$#$1 . canonicalize_path($2)#e;
1281 - return $url;
1282 -}
1283 -
1265 # get_svnprops(PATH)
1266 # ------------------
1267 # Helper for cmd_propget and cmd_proplist below.
perl/Git/SVN/Utils.pm
+51 -1
@@ -5,7 +5,12 @@ use warnings;
5
6 use base qw(Exporter);
7
8 -our @EXPORT_OK = qw(fatal can_compress);
8 +our @EXPORT_OK = qw(
9 + fatal
10 + can_compress
11 + canonicalize_path
12 + canonicalize_url
13 +);
14
15
16 =head1 NAME
@@ -56,4 +61,49 @@ sub can_compress {
61 }
62
63
64 +=head3 canonicalize_path
65 +
66 + my $canoncalized_path = canonicalize_path($path);
67 +
68 +Converts $path into a canonical form which is safe to pass to the SVN
69 +API as a file path.
70 +
71 +=cut
72 +
73 +sub canonicalize_path {
74 + my ($path) = @_;
75 + my $dot_slash_added = 0;
76 + if (substr($path, 0, 1) ne "/") {
77 + $path = "./" . $path;
78 + $dot_slash_added = 1;
79 + }
80 + # File::Spec->canonpath doesn't collapse x/../y into y (for a
81 + # good reason), so let's do this manually.
82 + $path =~ s#/+#/#g;
83 + $path =~ s#/\.(?:/|$)#/#g;
84 + $path =~ s#/[^/]+/\.\.##g;
85 + $path =~ s#/$##g;
86 + $path =~ s#^\./## if $dot_slash_added;
87 + $path =~ s#^/##;
88 + $path =~ s#^\.$##;
89 + return $path;
90 +}
91 +
92 +
93 +=head3 canonicalize_url
94 +
95 + my $canonicalized_url = canonicalize_url($url);
96 +
97 +Converts $url into a canonical form which is safe to pass to the SVN
98 +API as a URL.
99 +
100 +=cut
101 +
102 +sub canonicalize_url {
103 + my ($url) = @_;
104 + $url =~ s#^([^:]+://[^/]*/)(.*)$#$1 . canonicalize_path($2)#e;
105 + return $url;
106 +}
107 +
108 +
109 1;