lib/App/Rgit/Utils.pm
t/00-load.t
t/15-failures.t
+t/16-levels.t
t/20-each.t
t/21-once.t
t/90-boilerplate.t
use Cwd qw/cwd/;
use File::Spec::Functions qw/catfile path/;
-use App::Rgit::Utils qw/:codes/;
+use App::Rgit::Utils qw/:codes :levels/;
use App::Rgit;
our $VERSION;
BEGIN {
@ARGV = grep {
defined $cmd ? $_
- : ( /^-([IKV]+)$/ ? do { $opts{$_} = 1 for split //, $1; () }
- : do { $cmd = $_ unless /^-/; $_ } )
+ : ( /^-([DIKV]+)$/ ? do { $opts{$_} = 1 for split //, $1; () }
+ : do { $cmd = $_ unless /^-/; $_ } )
} @ARGV;
- warn "rgit $VERSION\n" if $opts{V};
$cmd = ' ' unless defined $cmd;
}
my $root = $ENV{GIT_DIR};
$root = cwd unless defined $root;
-exit App::Rgit->new(
+my $ar = App::Rgit->new(
git => $git,
root => $root,
cmd => $cmd,
args => \@ARGV,
policy => \&policy,
-)->run;
+ debug => $opts{D} ? INFO : WARN,
+);
+
+print STDOUT "rgit $VERSION\n" if $opts{V};
+
+exit $ar->run;
sub policy_default {
my ($cmd, $conf, $repo, $status, $signal) = @_;
);
my $int = { GetControlChars() }->{INTERRUPT};
while (1) {
- print STDERR "[a]bort, [i]gnore, [I]gnore all, [r]etry, open [s]hell ?";
+ $conf->warn("[a]bort, [i]gnore, [I]gnore all, [r]etry, open [s]hell ?");
ReadMode(4);
my $key = ReadKey(0);
ReadMode(1);
print STDERR "\n";
next unless defined $key;
if ($key eq $int) {
- print STDERR "Interrupted, aborting\n";
+ $conf->warn("Interrupted, aborting\n");
return LAST;
} elsif ($key eq 's') {
if (defined $shell) {
- print STDERR 'Opening shell in ', $repo->work, "\n";
+ $conf->info('Opening shell in ', $repo->work, "\n");
my $cwd = cwd;
$repo->chdir;
system { $shell } $shell;
chroot $cwd;
} else {
- print STDERR "Couldn't find any shell\n";
+ $conf->err("Couldn't find any shell\n");
}
} elsif (exists $codes{$key}) {
my $code = $codes{$key};
- print STDERR 'Okay, ', $code->[1], "\n";
+ $conf->info('Okay, ', $code->[1], "\n");
return $code->[0];
}
}
=head1 SYNOPSIS
- rgit [-K|-V|-I] [GIT_OPTIONS] COMMAND [COMMAND_ARGS]
+ rgit [-K|-I|-D|-V] [GIT_OPTIONS] COMMAND [COMMAND_ARGS]
=head1 DESCRIPTION
=item *
+C<-D>
+
+Outputs diagnostics.
+
+=item *
+
C<-V>
Outputs the version.
sub new {
my ($class, %args) = &validate;
my $config = App::Rgit::Config->new(
- root => $args{root},
- git => $args{git},
+ root => $args{root},
+ git => $args{git},
+ debug => $args{debug},
);
return unless defined $config;
my $command = App::Rgit::Command->new(
use Cwd qw/abs_path/;
use File::Spec::Functions qw/file_name_is_absolute/;
-use Object::Tiny qw/root git cwd_repo/;
+use Object::Tiny qw/root git cwd_repo debug/;
use App::Rgit::Repository;
-use App::Rgit::Utils qw/validate/;
+use App::Rgit::Utils qw/validate :levels/;
=head1 NAME
root => $root,
git => $args{git},
cwd_repo => $r,
+ debug => defined $args{debug} ? int $args{debug} : WARN,
);
}
+=head2 C<info $msg>
+
+=head2 C<warn $msg>
+
+=head2 C<err $msg>
+
+=head2 C<crit $msg>
+
+Notifies a message C<$msg> of the corresponding level.
+
+=cut
+
+sub _notify {
+ my $self = shift;
+ my $level = shift;
+ if ($self->debug >= $level) {
+ print STDERR @_;
+ return 1;
+ }
+ return 0;
+}
+
+sub info { shift->_notify(INFO, @_) }
+
+sub warn { shift->_notify(WARN, @_) }
+
+sub err { shift->_notify(ERR, @_) }
+
+sub crit { shift->_notify(CRIT, @_) }
+
=head2 C<root>
=head2 C<git>
=head2 C<cwd_repo>
+=head2 C<debug>
+
Accessors.
=head1 SEE ALSO
system { $conf->git } $conf->git, @args;
}
if ($? == -1) {
- warn "Failed to execute git: $!\n";
+ $conf->crit("Failed to execute git: $!\n");
return;
}
my $ret;
my $sig;
if (WIFSIGNALED($?)) {
$sig = WTERMSIG($?);
- warn "git died with signal $sig\n";
+ $conf->warn("git died with signal $sig\n");
if ($sig == SIGINT || $sig == SIGQUIT) {
- warn "Aborting.\n";
+ $conf->err("Aborting\n");
exit $sig;
}
} elsif ($ret) {
- warn "git returned $ret\n";
+ $conf->info("git returned $ret\n");
}
return wantarray ? ($ret, $sig) : $ret;
}
LAST => 0x8,
};
+=head2 C<DIAG>, C<INFO>, C<WARN>, C<ERR> and C<CRIT>
+
+Message levels.
+
+=cut
+
+use constant {
+ INFO => 3,
+ WARN => 2,
+ ERR => 1,
+ CRIT => 0,
+};
+
=head1 FUNCTIONS
=head2 C<validate @method_args>
C<NEXT> C<REDO>, C<LAST> and C<SAVE> are only exported on request, either by their name or by the C<'codes'> tags.
+C<INFO>, C<WARN>, C<ERR> and C<CRIT> are only exported on request, either by their name or by the C<'levels'> tags.
+
=cut
use base qw/Exporter/;
our @EXPORT = ();
our %EXPORT_TAGS = (
- funcs => [ qw/validate/ ],
- codes => [ qw/SAVE NEXT REDO LAST/ ],
+ funcs => [ qw/validate/ ],
+ codes => [ qw/SAVE NEXT REDO LAST/ ],
+ levels => [ qw/INFO WARN ERR CRIT/ ],
);
our @EXPORT_OK = map { @$_ } values %EXPORT_TAGS;
$EXPORT_TAGS{'all'} = [ @EXPORT_OK ];
--- /dev/null
+#!perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 4;
+
+use App::Rgit::Config;
+use App::Rgit::Utils qw/:levels/;
+
+local $SIG{__WARN__} = sub { diag @_ };
+local $SIG{__DIE__} = sub { diag @_ };
+
+my %levels = (
+ info => INFO,
+ warn => WARN,
+ err => ERR,
+ crit => CRIT,
+);
+my @levels = sort { $levels{$b} <=> $levels{$a} } keys %levels;
+
+my $olderr;
+open $olderr, '>&', \*STDERR or die "Can't dup STDERR: $!";
+
+for my $l (0 .. $#levels) {
+ my $arc = App::Rgit::Config->new(
+ root => 't',
+ git => 't/bin/git',
+ debug => $levels{$levels[$l]}
+ );
+ my $buf = '';
+ close STDERR;
+ open STDERR, '>', \$buf or die "open(STDERR, '>', \\\$buf): $!";
+ $arc->$_($_) for qw/info warn err crit/;
+ is($buf, join('', @levels[$l .. $#levels]), "level $l ok");
+}
+
+close STDERR;
+open STDERR, '>&', $olderr or die "Can't dup \$olderr: $!";