Skip to content

Commit c2768fa

Browse files
schwernEric Wong
authored andcommitted
Extract some utilities from git-svn to allow extracting Git::SVN.
Put them in a new module called Git::SVN::Utils. Yeah, not terribly original and it will be a dumping ground. But its better than having them in the main git-svn program. At least they can be documented and tested. * fatal() is used by many classes. * Change the $can_compress lexical into a function. This should be enough to extract Git::SVN. Signed-off-by: Michael G. Schwern <schwern@pobox.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Eric Wong <normalperson@yhbt.net>
1 parent ee9be06 commit c2768fa

File tree

6 files changed

+132
-15
lines changed

6 files changed

+132
-15
lines changed

git-svn.perl

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
$AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
1111
$VERSION = '@@GIT_VERSION@@';
1212

13+
use Git::SVN::Utils qw(fatal can_compress);
14+
1315
# From which subdir have we been invoked?
1416
my $cmd_dir_prefix = eval {
1517
command_oneline([qw/rev-parse --show-prefix/], STDERR => 0)
@@ -35,8 +37,6 @@
3537
$ENV{TZ} = 'UTC';
3638
$| = 1; # unbuffer STDOUT
3739

38-
sub fatal (@) { print STDERR "@_\n"; exit 1 }
39-
4040
# All SVN commands do it. Otherwise we may die on SIGPIPE when the remote
4141
# repository decides to close the connection which we expect to be kept alive.
4242
$SIG{PIPE} = 'IGNORE';
@@ -66,7 +66,7 @@ sub _req_svn {
6666
fatal "Need SVN::Core 1.1.0 or better (got $SVN::Core::VERSION)";
6767
}
6868
}
69-
my $can_compress = eval { require Compress::Zlib; 1};
69+
7070
use Carp qw/croak/;
7171
use Digest::MD5;
7272
use IO::File qw//;
@@ -1578,7 +1578,7 @@ sub cmd_reset {
15781578
}
15791579

15801580
sub cmd_gc {
1581-
if (!$can_compress) {
1581+
if (!can_compress()) {
15821582
warn "Compress::Zlib could not be found; unhandled.log " .
15831583
"files will not be compressed.\n";
15841584
}
@@ -2013,13 +2013,13 @@ sub md5sum {
20132013
} elsif (!$ref) {
20142014
$md5->add($arg) or croak $!;
20152015
} else {
2016-
::fatal "Can't provide MD5 hash for unknown ref type: '", $ref, "'";
2016+
fatal "Can't provide MD5 hash for unknown ref type: '", $ref, "'";
20172017
}
20182018
return $md5->hexdigest();
20192019
}
20202020

20212021
sub gc_directory {
2022-
if ($can_compress && -f $_ && basename($_) eq "unhandled.log") {
2022+
if (can_compress() && -f $_ && basename($_) eq "unhandled.log") {
20232023
my $out_filename = $_ . ".gz";
20242024
open my $in_fh, "<", $_ or die "Unable to open $_: $!\n";
20252025
binmode $in_fh;
@@ -2054,6 +2054,9 @@ package Git::SVN;
20542054
use Memoize; # core since 5.8.0, Jul 2002
20552055
use Memoize::Storable;
20562056
use POSIX qw(:signal_h);
2057+
2058+
use Git::SVN::Utils qw(fatal can_compress);
2059+
20572060
my $can_use_yaml;
20582061
BEGIN {
20592062
$can_use_yaml = eval { require Git::SVN::Memoize::YAML; 1};
@@ -2879,8 +2882,8 @@ sub assert_index_clean {
28792882
command_noisy('read-tree', $treeish);
28802883
$x = command_oneline('write-tree');
28812884
if ($y ne $x) {
2882-
::fatal "trees ($treeish) $y != $x\n",
2883-
"Something is seriously wrong...";
2885+
fatal "trees ($treeish) $y != $x\n",
2886+
"Something is seriously wrong...";
28842887
}
28852888
});
28862889
}
@@ -3235,7 +3238,7 @@ sub mkemptydirs {
32353238
my %empty_dirs = ();
32363239
my $gz_file = "$self->{dir}/unhandled.log.gz";
32373240
if (-f $gz_file) {
3238-
if (!$can_compress) {
3241+
if (!can_compress()) {
32393242
warn "Compress::Zlib could not be found; ",
32403243
"empty directories in $gz_file will not be read\n";
32413244
} else {
@@ -3918,7 +3921,7 @@ sub set_tree {
39183921
my ($self, $tree) = (shift, shift);
39193922
my $log_entry = ::get_commit_entry($tree);
39203923
unless ($self->{last_rev}) {
3921-
::fatal("Must have an existing revision to commit");
3924+
fatal("Must have an existing revision to commit");
39223925
}
39233926
my %ed_opts = ( r => $self->{last_rev},
39243927
log => $log_entry->{log},
@@ -4347,6 +4350,7 @@ sub remove_username {
43474350
package Git::SVN::Log;
43484351
use strict;
43494352
use warnings;
4353+
use Git::SVN::Utils qw(fatal);
43504354
use POSIX qw/strftime/;
43514355
use constant commit_log_separator => ('-' x 72) . "\n";
43524356
use vars qw/$TZ $limit $color $pager $non_recursive $verbose $oneline
@@ -4445,15 +4449,15 @@ sub config_pager {
44454449
sub run_pager {
44464450
return unless defined $pager;
44474451
pipe my ($rfd, $wfd) or return;
4448-
defined(my $pid = fork) or ::fatal "Can't fork: $!";
4452+
defined(my $pid = fork) or fatal "Can't fork: $!";
44494453
if (!$pid) {
44504454
open STDOUT, '>&', $wfd or
4451-
::fatal "Can't redirect to stdout: $!";
4455+
fatal "Can't redirect to stdout: $!";
44524456
return;
44534457
}
4454-
open STDIN, '<&', $rfd or ::fatal "Can't redirect stdin: $!";
4458+
open STDIN, '<&', $rfd or fatal "Can't redirect stdin: $!";
44554459
$ENV{LESS} ||= 'FRSX';
4456-
exec $pager or ::fatal "Can't run pager: $! ($pager)";
4460+
exec $pager or fatal "Can't run pager: $! ($pager)";
44574461
}
44584462

44594463
sub format_svn_date {
@@ -4602,7 +4606,7 @@ sub cmd_show_log {
46024606
} elsif ($::_revision =~ /^\d+$/) {
46034607
$r_min = $r_max = $::_revision;
46044608
} else {
4605-
::fatal "-r$::_revision is not supported, use ",
4609+
fatal "-r$::_revision is not supported, use ",
46064610
"standard 'git log' arguments instead";
46074611
}
46084612
}

perl/Git/SVN/Utils.pm

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package Git::SVN::Utils;
2+
3+
use strict;
4+
use warnings;
5+
6+
use base qw(Exporter);
7+
8+
our @EXPORT_OK = qw(fatal can_compress);
9+
10+
11+
=head1 NAME
12+
13+
Git::SVN::Utils - utility functions used across Git::SVN
14+
15+
=head1 SYNOPSIS
16+
17+
use Git::SVN::Utils qw(functions to import);
18+
19+
=head1 DESCRIPTION
20+
21+
This module contains functions which are useful across many different
22+
parts of Git::SVN. Mostly it's a place to put utility functions
23+
rather than duplicate the code or have classes grabbing at other
24+
classes.
25+
26+
=head1 FUNCTIONS
27+
28+
All functions can be imported only on request.
29+
30+
=head3 fatal
31+
32+
fatal(@message);
33+
34+
Display a message and exit with a fatal error code.
35+
36+
=cut
37+
38+
# Note: not certain why this is in use instead of die. Probably because
39+
# the exit code of die is 255? Doesn't appear to be used consistently.
40+
sub fatal (@) { print STDERR "@_\n"; exit 1 }
41+
42+
43+
=head3 can_compress
44+
45+
my $can_compress = can_compress;
46+
47+
Returns true if Compress::Zlib is available, false otherwise.
48+
49+
=cut
50+
51+
my $can_compress;
52+
sub can_compress {
53+
return $can_compress if defined $can_compress;
54+
55+
return $can_compress = eval { require Compress::Zlib; };
56+
}
57+
58+
59+
1;

perl/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ modules += Git/SVN/Fetcher
3434
modules += Git/SVN/Editor
3535
modules += Git/SVN/Prompt
3636
modules += Git/SVN/Ra
37+
modules += Git/SVN/Utils
3738

3839
$(makfile): ../GIT-CFLAGS Makefile
3940
echo all: private-Error.pm Git.pm Git/I18N.pm > $@

t/Git-SVN/00compile.t

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env perl
2+
3+
use strict;
4+
use warnings;
5+
6+
use Test::More tests => 1;
7+
8+
require_ok 'Git::SVN::Utils';

t/Git-SVN/Utils/can_compress.t

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/perl
2+
3+
use strict;
4+
use warnings;
5+
6+
use Test::More 'no_plan';
7+
8+
use Git::SVN::Utils qw(can_compress);
9+
10+
# !! is the "convert this to boolean" operator.
11+
is !!can_compress(), !!eval { require Compress::Zlib };

t/Git-SVN/Utils/fatal.t

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/perl
2+
3+
use strict;
4+
use warnings;
5+
6+
use Test::More 'no_plan';
7+
8+
BEGIN {
9+
# Override exit at BEGIN time before Git::SVN::Utils is loaded
10+
# so it will see our local exit later.
11+
*CORE::GLOBAL::exit = sub(;$) {
12+
return @_ ? CORE::exit($_[0]) : CORE::exit();
13+
};
14+
}
15+
16+
use Git::SVN::Utils qw(fatal);
17+
18+
# fatal()
19+
{
20+
# Capture the exit code and prevent exit.
21+
my $exit_status;
22+
no warnings 'redefine';
23+
local *CORE::GLOBAL::exit = sub { $exit_status = $_[0] || 0 };
24+
25+
# Trap fatal's message to STDERR
26+
my $stderr;
27+
close STDERR;
28+
ok open STDERR, ">", \$stderr;
29+
30+
fatal "Some", "Stuff", "Happened";
31+
32+
is $stderr, "Some Stuff Happened\n";
33+
is $exit_status, 1;
34+
}

0 commit comments

Comments
 (0)