1package Git::SVN::Utils;
23
use strict;
4use warnings;
56
use base qw(Exporter);
78
our @EXPORT_OK = qw(
9fatal
10can_compress
11canonicalize_path
12canonicalize_url
13);
1415
16
=head1 NAME
1718
Git::SVN::Utils - utility functions used across Git::SVN
1920
=head1 SYNOPSIS
2122
use Git::SVN::Utils qw(functions to import);
2324
=head1 DESCRIPTION
2526
This module contains functions which are useful across many different
27parts of Git::SVN. Mostly it's a place to put utility functions
28rather than duplicate the code or have classes grabbing at other
29classes.
3031
=head1 FUNCTIONS
3233
All functions can be imported only on request.
3435
=head3 fatal
3637
fatal(@message);
3839
Display a message and exit with a fatal error code.
4041
=cut
4243
# Note: not certain why this is in use instead of die. Probably because
44# the exit code of die is 255? Doesn't appear to be used consistently.
45sub fatal (@) { print STDERR "@_\n"; exit 1 }
4647
48
=head3 can_compress
4950
my $can_compress = can_compress;
5152
Returns true if Compress::Zlib is available, false otherwise.
5354
=cut
5556
my $can_compress;
57sub can_compress {
58return $can_compress if defined $can_compress;
5960
return $can_compress = eval { require Compress::Zlib; };
61}
6263
64
=head3 canonicalize_path
6566
my $canoncalized_path = canonicalize_path($path);
6768
Converts $path into a canonical form which is safe to pass to the SVN
69API as a file path.
7071
=cut
7273
sub 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 a
81# 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}
9192
93
=head3 canonicalize_url
9495
my $canonicalized_url = canonicalize_url($url);
9697
Converts $url into a canonical form which is safe to pass to the SVN
98API as a URL.
99100
=cut
101102
sub canonicalize_url {
103my ($url) = @_;
104$url =~ s#^([^:]+://[^/]*/)(.*)$#$1 . canonicalize_path($2)#e;
105return $url;
106}
107108
109
1;