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); 16 17 18=head1 NAME 19 20Git::SVN::Utils - utility functions used across Git::SVN 21 22=head1 SYNOPSIS 23 24 use Git::SVN::Utils qw(functions to import); 25 26=head1 DESCRIPTION 27 28This module contains functions which are useful across many different 29parts of Git::SVN. Mostly it's a place to put utility functions 30rather than duplicate the code or have classes grabbing at other 31classes. 32 33=head1 FUNCTIONS 34 35All functions can be imported only on request. 36 37=head3 fatal 38 39 fatal(@message); 40 41Display a message and exit with a fatal error code. 42 43=cut 44 45# Note: not certain why this is in use instead of die. Probably because 46# the exit code of die is 255? Doesn't appear to be used consistently. 47sub fatal (@) { print STDERR "@_\n"; exit 1 } 48 49 50=head3 can_compress 51 52 my $can_compress = can_compress; 53 54Returns true if Compress::Zlib is available, false otherwise. 55 56=cut 57 58my $can_compress; 59sub can_compress { 60 return $can_compress if defined $can_compress; 61 62 return $can_compress = eval { require Compress::Zlib; }; 63} 64 65 66=head3 canonicalize_path 67 68 my $canoncalized_path = canonicalize_path($path); 69 70Converts $path into a canonical form which is safe to pass to the SVN 71API as a file path. 72 73=cut 74 75sub canonicalize_path { 76 my ($path) = @_; 77 my $dot_slash_added = 0; 78 if (substr($path, 0, 1) ne "/") { 79 $path = "./" . $path; 80 $dot_slash_added = 1; 81 } 82 # File::Spec->canonpath doesn't collapse x/../y into y (for a 83 # good reason), so let's do this manually. 84 $path =~ s#/+#/#g; 85 $path =~ s#/\.(?:/|$)#/#g; 86 $path =~ s#/[^/]+/\.\.##g; 87 $path =~ s#/$##g; 88 $path =~ s#^\./## if $dot_slash_added; 89 $path =~ s#^/##; 90 $path =~ s#^\.$##; 91 return $path; 92} 93 94 95=head3 canonicalize_url 96 97 my $canonicalized_url = canonicalize_url($url); 98 99Converts $url into a canonical form which is safe to pass to the SVN 100API as a URL. 101 102=cut 103 104sub canonicalize_url { 105 my $url = shift; 106 107 # The 1.7 way to do it 108 if ( defined &SVN::_Core::svn_uri_canonicalize ) { 109 return SVN::_Core::svn_uri_canonicalize($url); 110 } 111 # There wasn't a 1.6 way to do it, so we do it ourself. 112 else { 113 return _canonicalize_url_ourselves($url); 114 } 115} 116 117 118sub _canonicalize_url_ourselves { 119 my ($url) = @_; 120 $url =~ s#^([^:]+://[^/]*/)(.*)$#$1 . canonicalize_path($2)#e; 121 return $url; 122} 123 124 1251;