2012-07-27 01:22:22 +02:00
|
|
|
package Git::SVN::Utils;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use base qw(Exporter);
|
|
|
|
|
2012-07-28 11:38:26 +02:00
|
|
|
our @EXPORT_OK = qw(
|
|
|
|
fatal
|
|
|
|
can_compress
|
|
|
|
canonicalize_path
|
|
|
|
canonicalize_url
|
|
|
|
);
|
2012-07-27 01:22:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
Git::SVN::Utils - utility functions used across Git::SVN
|
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
|
|
|
use Git::SVN::Utils qw(functions to import);
|
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
This module contains functions which are useful across many different
|
|
|
|
parts of Git::SVN. Mostly it's a place to put utility functions
|
|
|
|
rather than duplicate the code or have classes grabbing at other
|
|
|
|
classes.
|
|
|
|
|
|
|
|
=head1 FUNCTIONS
|
|
|
|
|
|
|
|
All functions can be imported only on request.
|
|
|
|
|
|
|
|
=head3 fatal
|
|
|
|
|
|
|
|
fatal(@message);
|
|
|
|
|
|
|
|
Display a message and exit with a fatal error code.
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
# Note: not certain why this is in use instead of die. Probably because
|
|
|
|
# the exit code of die is 255? Doesn't appear to be used consistently.
|
|
|
|
sub fatal (@) { print STDERR "@_\n"; exit 1 }
|
|
|
|
|
|
|
|
|
|
|
|
=head3 can_compress
|
|
|
|
|
|
|
|
my $can_compress = can_compress;
|
|
|
|
|
|
|
|
Returns true if Compress::Zlib is available, false otherwise.
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
my $can_compress;
|
|
|
|
sub can_compress {
|
|
|
|
return $can_compress if defined $can_compress;
|
|
|
|
|
|
|
|
return $can_compress = eval { require Compress::Zlib; };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-28 11:38:26 +02:00
|
|
|
=head3 canonicalize_path
|
|
|
|
|
|
|
|
my $canoncalized_path = canonicalize_path($path);
|
|
|
|
|
|
|
|
Converts $path into a canonical form which is safe to pass to the SVN
|
|
|
|
API as a file path.
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
sub canonicalize_path {
|
|
|
|
my ($path) = @_;
|
|
|
|
my $dot_slash_added = 0;
|
|
|
|
if (substr($path, 0, 1) ne "/") {
|
|
|
|
$path = "./" . $path;
|
|
|
|
$dot_slash_added = 1;
|
|
|
|
}
|
|
|
|
# File::Spec->canonpath doesn't collapse x/../y into y (for a
|
|
|
|
# good reason), so let's do this manually.
|
|
|
|
$path =~ s#/+#/#g;
|
|
|
|
$path =~ s#/\.(?:/|$)#/#g;
|
|
|
|
$path =~ s#/[^/]+/\.\.##g;
|
|
|
|
$path =~ s#/$##g;
|
|
|
|
$path =~ s#^\./## if $dot_slash_added;
|
|
|
|
$path =~ s#^/##;
|
|
|
|
$path =~ s#^\.$##;
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
=head3 canonicalize_url
|
|
|
|
|
|
|
|
my $canonicalized_url = canonicalize_url($url);
|
|
|
|
|
|
|
|
Converts $url into a canonical form which is safe to pass to the SVN
|
|
|
|
API as a URL.
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
sub canonicalize_url {
|
|
|
|
my ($url) = @_;
|
|
|
|
$url =~ s#^([^:]+://[^/]*/)(.*)$#$1 . canonicalize_path($2)#e;
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-27 01:22:22 +02:00
|
|
|
1;
|