1package Git::SVN::Utils; 2 3use strict; 4use warnings; 5 6use SVN::Core; 7 8use base qw(Exporter); 9 10our@EXPORT_OK=qw( 11 fatal 12 can_compress 13 canonicalize_path 14 canonicalize_url 15 join_paths 16 add_path_to_url 17); 18 19 20=head1 NAME 21 22Git::SVN::Utils - utility functions used across Git::SVN 23 24=head1 SYNOPSIS 25 26 use Git::SVN::Utils qw(functions to import); 27 28=head1 DESCRIPTION 29 30This module contains functions which are useful across many different 31parts of Git::SVN. Mostly it's a place to put utility functions 32rather than duplicate the code or have classes grabbing at other 33classes. 34 35=head1 FUNCTIONS 36 37All functions can be imported only on request. 38 39=head3 fatal 40 41 fatal(@message); 42 43Display a message and exit with a fatal error code. 44 45=cut 46 47# Note: not certain why this is in use instead of die. Probably because 48# the exit code of die is 255? Doesn't appear to be used consistently. 49sub fatal (@) {print STDERR "@_\n";exit1} 50 51 52=head3 can_compress 53 54 my $can_compress = can_compress; 55 56Returns true if Compress::Zlib is available, false otherwise. 57 58=cut 59 60my$can_compress; 61sub can_compress { 62return$can_compressifdefined$can_compress; 63 64return$can_compress=eval{require Compress::Zlib; }; 65} 66 67 68=head3 canonicalize_path 69 70 my $canoncalized_path = canonicalize_path($path); 71 72Converts $path into a canonical form which is safe to pass to the SVN 73API as a file path. 74 75=cut 76 77# Turn foo/../bar into bar 78sub _collapse_dotdot { 79my$path=shift; 80 811while$path=~s{/[^/]+/+\.\.}{}; 821while$path=~s{[^/]+/+\.\./}{}; 831while$path=~s{[^/]+/+\.\.}{}; 84 85return$path; 86} 87 88 89sub canonicalize_path { 90my$path=shift; 91my$rv; 92 93# The 1.7 way to do it 94if(defined&SVN::_Core::svn_dirent_canonicalize ) { 95$path= _collapse_dotdot($path); 96$rv= SVN::_Core::svn_dirent_canonicalize($path); 97} 98# The 1.6 way to do it 99# This can return undef on subversion-perl-1.4.2-2.el5 (CentOS 5.2) 100elsif(defined&SVN::_Core::svn_path_canonicalize ) { 101$path= _collapse_dotdot($path); 102$rv= SVN::_Core::svn_path_canonicalize($path); 103} 104 105return$rvifdefined$rv; 106 107# No SVN API canonicalization is available, or the SVN API 108# didn't return a successful result, do it ourselves 109return _canonicalize_path_ourselves($path); 110} 111 112 113sub _canonicalize_path_ourselves { 114my($path) =@_; 115my$dot_slash_added=0; 116if(substr($path,0,1)ne"/") { 117$path="./".$path; 118$dot_slash_added=1; 119} 120$path=~ s#/+#/#g; 121$path=~ s#/\.(?:/|$)#/#g; 122$path= _collapse_dotdot($path); 123$path=~ s#/$##g; 124$path=~ s#^\./## if $dot_slash_added; 125$path=~ s#^/##; 126$path=~ s#^\.$##; 127return$path; 128} 129 130 131=head3 canonicalize_url 132 133 my $canonicalized_url = canonicalize_url($url); 134 135Converts $url into a canonical form which is safe to pass to the SVN 136API as a URL. 137 138=cut 139 140sub canonicalize_url { 141my$url=shift; 142 143# The 1.7 way to do it 144if(defined&SVN::_Core::svn_uri_canonicalize ) { 145return SVN::_Core::svn_uri_canonicalize($url); 146} 147# There wasn't a 1.6 way to do it, so we do it ourself. 148else{ 149return _canonicalize_url_ourselves($url); 150} 151} 152 153 154sub _canonicalize_url_path { 155my($uri_path) =@_; 156 157my@parts; 158foreachmy$part(splitm{/+},$uri_path) { 159$part=~s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg; 160push@parts,$part; 161} 162 163returnjoin('/',@parts); 164} 165 166sub _canonicalize_url_ourselves { 167my($url) =@_; 168if($url=~ m#^([^:]+)://([^/]*)(.*)$#) { 169my($scheme,$domain,$uri) = ($1,$2, _canonicalize_url_path(canonicalize_path($3))); 170$url="$scheme://$domain$uri"; 171} 172$url; 173} 174 175 176=head3 join_paths 177 178 my $new_path = join_paths(@paths); 179 180Appends @paths together into a single path. Any empty paths are ignored. 181 182=cut 183 184sub join_paths { 185my@paths=@_; 186 187@paths=grep{defined$_&&length$_}@paths; 188 189return''unless@paths; 190return$paths[0]if@paths==1; 191 192my$new_path=shift@paths; 193$new_path=~s{/+$}{}; 194 195my$last_path=pop@paths; 196$last_path=~s{^/+}{}; 197 198formy$path(@paths) { 199$path=~s{^/+}{}; 200$path=~s{/+$}{}; 201$new_path.="/$path"; 202} 203 204return$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 212Appends $path onto the $url. If $path is empty, $url is returned unchanged. 213 214=cut 215 216sub add_path_to_url { 217my($url,$path) =@_; 218 219return$urlif!defined$pathor!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 230returnjoin'/',$url,$path; 231} 232 2331;