X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=lib%2FApp%2FRgit%2FRepository.pm;h=df44edc754306d22f89605391dd73ec3fc614366;hb=517c579bda9717613f7faa9ffe41ef1648199e9b;hp=f7b03d7fd799964ffecc042a330c52ec5e9b32df;hpb=3051ac76178c03ab7a6e1c1e87a2533a84a738b7;p=perl%2Fmodules%2Frgit.git diff --git a/lib/App/Rgit/Repository.pm b/lib/App/Rgit/Repository.pm index f7b03d7..df44edc 100644 --- a/lib/App/Rgit/Repository.pm +++ b/lib/App/Rgit/Repository.pm @@ -3,15 +3,21 @@ package App::Rgit::Repository; use strict; use warnings; -use Cwd qw/cwd abs_path/; -use File::Spec::Functions qw/canonpath catdir splitdir abs2rel file_name_is_absolute/; -use POSIX qw/WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG SIGINT SIGQUIT/; +use Cwd (); # cwd, abs_path +use File::Spec (); # canonpath, catdir, splitdir, abs2rel +use POSIX (); # WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG SIGINT SIGQUIT + +my ($WIFEXITED, $WEXITSTATUS, $WIFSIGNALED, $WTERMSIG); BEGIN { - no warnings 'redefine'; - *WIFEXITED = sub { 1 } unless eval { WIFEXITED(0); 1 }; - *WEXITSTATUS = sub { shift() >> 8 } unless eval { WEXITSTATUS(0); 1 }; - *WIFSIGNALED = sub { shift() & 127 } unless eval { WIFSIGNALED(0); 1 }; + $WIFEXITED = eval { POSIX::WIFEXITED(0); 1 } ? \&POSIX::WIFEXITED + : sub { 1 }; + $WEXITSTATUS = eval { POSIX::WEXITSTATUS(0); 1 } ? \&POSIX::WEXITSTATUS + : sub { shift() >> 8 }; + $WIFSIGNALED = eval { POSIX::WIFSIGNALED(0); 1 } ? \&POSIX::WIFSIGNALED + : sub { shift() & 127 }; + $WTERMSIG = eval { POSIX::WTERMSIG(0); 1 } ? \&POSIX::WTERMSIG + : sub { shift() & 127 }; } =head1 NAME @@ -48,9 +54,12 @@ sub new { my %args = @_; my $dir = $args{dir}; - $dir = abs_path $dir if defined $dir and not file_name_is_absolute $dir; - $dir = cwd unless defined $dir; - $dir = canonpath $dir; + if (defined $dir) { + $dir = Cwd::abs_path($dir); + } else { + $dir = Cwd::cwd; + } + $dir = File::Spec->canonpath($dir); my ($repo, $bare, $name, $work); if ($args{fake}) { @@ -58,21 +67,21 @@ sub new { } else { return unless -d $dir and -d "$dir/refs" - and -d "$_/objects" - and -e "$_/HEAD"; + and -d "$dir/objects" + and -e "$dir/HEAD"; - my @chunks = splitdir $dir; + my @chunks = File::Spec->splitdir($dir); my $last = pop @chunks; return unless defined $last; - if ($last eq '.git') { + if (@chunks and $last eq '.git') { $bare = 0; $name = $chunks[-1]; - $work = catdir @chunks; + $work = File::Spec->catdir(@chunks); } elsif ($last =~ /(.+)\.git$/) { $bare = 1; $name = $1; - $work = catdir @chunks, $last; + $work = File::Spec->catdir(@chunks, $last); } else { return; } @@ -113,22 +122,25 @@ Returns the exit code. =cut -sub _abs2rel { - my $a = &abs2rel; +my $abs2rel = sub { + my $a = File::Spec->abs2rel(@_); $a = $_[0] unless defined $a; $a; -} +}; my %escapes = ( '%' => sub { '%' }, 'n' => sub { shift->name }, - 'g' => sub { _abs2rel(shift->repo, shift->root) }, + 'g' => sub { $abs2rel->(shift->repo, shift->root) }, 'G' => sub { shift->repo }, - 'w' => sub { _abs2rel(shift->work, shift->root) }, + 'w' => sub { $abs2rel->(shift->work, shift->root) }, 'W' => sub { shift->work }, 'b' => sub { my ($self, $conf) = @_; - _abs2rel($self->bare ? $self->repo : $self->work . '.git', $conf->root) + $abs2rel->( + $self->bare ? $self->repo : $self->work . '.git', + $conf->root + ); }, 'B' => sub { $_[0]->bare ? $_[0]->repo : $_[0]->work . '.git' }, 'R' => sub { $_[1]->root }, @@ -140,34 +152,41 @@ sub run { my $self = shift; my $conf = shift; return unless $conf->isa('App::Rgit::Config'); + my @args = @_; + unless ($self->fake) { s/%($e)/$escapes{$1}->($self, $conf)/eg for @args; } + unshift @args, $conf->git; $conf->info('Executing "', join(' ', @args), '" into ', $self->work, "\n"); + { local $ENV{GIT_DIR} = $self->repo if exists $ENV{GIT_DIR}; local $ENV{GIT_EXEC_PATH} = $conf->git if exists $ENV{GIT_EXEC_PATH}; system { $args[0] } @args; } + if ($? == -1) { $conf->crit("Failed to execute git: $!\n"); return; } + my $ret; - $ret = WEXITSTATUS($?) if WIFEXITED($?); + $ret = $WEXITSTATUS->($?) if $WIFEXITED->($?); my $sig; - if (WIFSIGNALED($?)) { - $sig = WTERMSIG($?); + if ($WIFSIGNALED->($?)) { + $sig = $WTERMSIG->($?); $conf->warn("git died with signal $sig\n"); - if ($sig == SIGINT || $sig == SIGQUIT) { + if ($sig == POSIX::SIGINT || $sig == POSIX::SIGQUIT) { $conf->err("Aborting\n"); exit $sig; } } elsif ($ret) { $conf->info("git returned $ret\n"); } + return wantarray ? ($ret, $sig) : $ret; } @@ -211,7 +230,7 @@ You can find documentation for this module with the perldoc command. =head1 COPYRIGHT & LICENSE -Copyright 2008-2009 Vincent Pit, all rights reserved. +Copyright 2008,2009,2010 Vincent Pit, all rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.