X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=lib%2FApp%2FRgit%2FRepository.pm;h=f1dc149a628e2d635260631a82a9ef25a175c526;hb=e833c52a64e7aac9c18d7051285bf4d9906c4faf;hp=8a915b09f54fcc45b94274f5be74f8c1cd1e64cb;hpb=9fa31a712601956c8e4c34cbfc0af111346920c3;p=perl%2Fmodules%2Frgit.git diff --git a/lib/App/Rgit/Repository.pm b/lib/App/Rgit/Repository.pm index 8a915b0..f1dc149 100644 --- a/lib/App/Rgit/Repository.pm +++ b/lib/App/Rgit/Repository.pm @@ -3,20 +3,24 @@ 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 +use File::Spec (); # canonpath, catdir, splitdir, abs2rel +use POSIX (); # WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG SIGINT SIGQUIT -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 }; -} +use App::Rgit::Utils (); # abs_path -use Object::Tiny qw/fake repo bare name work/; +my ($WIFEXITED, $WEXITSTATUS, $WIFSIGNALED, $WTERMSIG); -use App::Rgit::Utils qw/validate/; +BEGIN { + $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 @@ -24,11 +28,11 @@ App::Rgit::Repository - Class representing a Git repository. =head1 VERSION -Version 0.05 +Version 0.08 =cut -our $VERSION = '0.05'; +our $VERSION = '0.08'; =head1 DESCRIPTION @@ -38,7 +42,12 @@ This is an internal class to L. =head1 METHODS -=head2 C<< new dir => $dir [, fake => 1 ] >> +=head2 C + + my $arr = App::Rgit::Repository->new( + dir => $dir, + fake => $bool, + ); Creates a new repository starting from C<$dir>. If the C option is passed, C<$dir> isn't checked to be a valid C repository. @@ -46,46 +55,54 @@ If the C option is passed, C<$dir> isn't checked to be a valid C repo =cut sub new { - my ($class, %args) = &validate; + my $class = shift; + $class = ref $class || $class; + + 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; + if (defined $dir) { + $dir = App::Rgit::Utils::abs_path($dir); + } else { + $dir = Cwd::cwd; + } + $dir = File::Spec->canonpath($dir); + my ($repo, $bare, $name, $work); if ($args{fake}) { $repo = $work = $dir; - } else { - my @tries = ($dir); - my @chunks = splitdir $dir; - my $last = pop @chunks; - push @tries, "$dir.git" unless $last =~ /\.git$/; - push @tries, catdir($dir, '.git') unless $last eq '.git'; - for (@tries) { - if (-d $_ && -d "$_/refs" and -d "$_/objects" and -e "$_/HEAD") { - $repo = $_; - last; - } - } - return unless defined $repo; - $repo = canonpath $repo; - @chunks = splitdir $repo; - $last = pop @chunks; - if ($last eq '.git') { + } else { + return unless -d $dir + and -d "$dir/refs" + and -d "$dir/objects" + and -e "$dir/HEAD"; + + my @chunks = File::Spec->splitdir($dir); + my $last = pop @chunks; + return unless defined $last; + + if (@chunks and $last eq '.git') { $bare = 0; $name = $chunks[-1]; - $work = catdir @chunks; - } else { + $work = File::Spec->catdir(@chunks); + } elsif ($last =~ /(.+)\.git$/) { $bare = 1; - ($name) = $last =~ /(.*)\.git$/; - $work = $repo; + $name = $1; + $work = File::Spec->catdir(@chunks, $last); + } else { + return; } + + $repo = $dir; } - $class->SUPER::new( + + bless { fake => !!$args{fake}, repo => $repo, bare => $bare, name => $name, work => $work, - ); + }, $class; } =head2 C @@ -104,7 +121,9 @@ sub chdir { return 1; } -=head2 C +=head2 C + + my $code = $arr->run($conf, @args); Runs C on the repository for the L configuration C<$conf>. When the repository isn't fake, the format substitutions applies to C<@args> elements. @@ -112,22 +131,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 }, @@ -139,34 +161,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; } @@ -180,7 +209,13 @@ sub run { =head2 C -Accessors. +Read-only accessors. + +=cut + +BEGIN { + eval "sub $_ { \$_[0]->{$_} }" for qw/fake repo bare name work/; +} =head1 SEE ALSO @@ -189,12 +224,13 @@ L. =head1 AUTHOR Vincent Pit, C<< >>, L. - + You can contact me by mail or on C (vincent). =head1 BUGS -Please report any bugs or feature requests to C, or through the web interface at L. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. +Please report any bugs or feature requests to C, or through the web interface at L. +I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. =head1 SUPPORT @@ -204,7 +240,7 @@ You can find documentation for this module with the perldoc command. =head1 COPYRIGHT & LICENSE -Copyright 2008 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.