1 package Git
::SVN
::Utils
;
21 Git::SVN::Utils - utility functions used across Git::SVN
25 use Git::SVN::Utils qw(functions to import);
29 This module contains functions which are useful across many different
30 parts of Git::SVN. Mostly it's a place to put utility functions
31 rather than duplicate the code or have classes grabbing at other
36 All functions can be imported only on request.
42 Display a message and exit with a fatal error code.
46 # Note: not certain why this is in use instead of die. Probably because
47 # the exit code of die is 255? Doesn't appear to be used consistently.
48 sub fatal
(@
) { print STDERR
"@_\n"; exit 1 }
53 my $can_compress = can_compress;
55 Returns true if Compress::Zlib is available, false otherwise.
61 return $can_compress if defined $can_compress;
63 return $can_compress = eval { require Compress
::Zlib
; };
67 =head3 canonicalize_path
69 my $canoncalized_path = canonicalize_path($path);
71 Converts $path into a canonical form which is safe to pass to the SVN
76 # Turn foo/../bar into bar
77 sub _collapse_dotdot
{
80 1 while $path =~ s{/[^/]+/+\.\.}{};
81 1 while $path =~ s{[^/]+/+\.\./}{};
82 1 while $path =~ s{[^/]+/+\.\.}{};
88 sub canonicalize_path
{
90 my $dot_slash_added = 0;
91 if (substr($path, 0, 1) ne "/") {
96 $path =~ s
#/\.(?:/|$)#/#g;
97 $path = _collapse_dotdot
($path);
99 $path =~ s
#^\./## if $dot_slash_added;
106 =head3 canonicalize_url
108 my $canonicalized_url = canonicalize_url($url);
110 Converts $url into a canonical form which is safe to pass to the SVN
115 sub canonicalize_url
{
118 # The 1.7 way to do it
119 if ( defined &SVN
::_Core
::svn_uri_canonicalize
) {
120 return SVN
::_Core
::svn_uri_canonicalize
($url);
122 # There wasn't a 1.6 way to do it, so we do it ourself.
124 return _canonicalize_url_ourselves
($url);
129 sub _canonicalize_url_ourselves
{
131 $url =~ s
#^([^:]+://[^/]*/)(.*)$#$1 . canonicalize_path($2)#e;
138 my $new_path = join_paths(@paths);
140 Appends @paths together into a single path. Any empty paths are ignored.
147 @paths = grep { defined $_ && length $_ } @paths;
149 return '' unless @paths;
150 return $paths[0] if @paths == 1;
152 my $new_path = shift @paths;
153 $new_path =~ s{/+$}{};
155 my $last_path = pop @paths;
156 $last_path =~ s{^/+}{};
158 for my $path (@paths) {
161 $new_path .= "/$path";
164 return $new_path .= "/$last_path";