1package Git::SVN::Utils;23use strict;4use warnings;56use base qw(Exporter);78our @EXPORT_OK = qw(9fatal10can_compress11canonicalize_path12canonicalize_url13);141516=head1 NAME1718Git::SVN::Utils - utility functions used across Git::SVN1920=head1 SYNOPSIS2122use Git::SVN::Utils qw(functions to import);2324=head1 DESCRIPTION2526This module contains functions which are useful across many different27parts of Git::SVN. Mostly it's a place to put utility functions28rather than duplicate the code or have classes grabbing at other29classes.3031=head1 FUNCTIONS3233All functions can be imported only on request.3435=head3 fatal3637fatal(@message);3839Display a message and exit with a fatal error code.4041=cut4243# Note: not certain why this is in use instead of die. Probably because44# the exit code of die is 255? Doesn't appear to be used consistently.45sub fatal (@) { print STDERR "@_\n"; exit 1 }464748=head3 can_compress4950my $can_compress = can_compress;5152Returns true if Compress::Zlib is available, false otherwise.5354=cut5556my $can_compress;57sub can_compress {58return $can_compress if defined $can_compress;5960return $can_compress = eval { require Compress::Zlib; };61}626364=head3 canonicalize_path6566my $canoncalized_path = canonicalize_path($path);6768Converts $path into a canonical form which is safe to pass to the SVN69API as a file path.7071=cut7273sub canonicalize_path {74my ($path) = @_;75my $dot_slash_added = 0;76if (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 a81# 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#^\.$##;89return $path;90}919293=head3 canonicalize_url9495my $canonicalized_url = canonicalize_url($url);9697Converts $url into a canonical form which is safe to pass to the SVN98API as a URL.99100=cut101102sub canonicalize_url {103my ($url) = @_;104$url =~ s#^([^:]+://[^/]*/)(.*)$#$1 . canonicalize_path($2)#e;105return $url;106}1071081091;