]> git.vpit.fr Git - perl/modules/rgit.git/blobdiff - lib/App/Rgit/Repository.pm
Always canonify the root, the git path and the repo directory
[perl/modules/rgit.git] / lib / App / Rgit / Repository.pm
index 93efb4cfa71eac2fa896ac83595cfb8c4a89368d..0ca5e940a692537d136620776cecb421e42461bb 100644 (file)
@@ -3,12 +3,16 @@ package App::Rgit::Repository;
 use strict;
 use warnings;
 
-use Cwd qw/cwd abs_path/;
-use File::Spec::Functions qw/catdir splitdir abs2rel/;
-
-use Object::Tiny qw/fake repo bare name work/;
-
-use App::Rgit::Utils qw/validate/;
+use Cwd                   qw/cwd abs_path/;
+use File::Spec::Functions qw/canonpath catdir splitdir abs2rel/;
+use POSIX qw/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 };
+}
 
 =head1 NAME
 
@@ -16,11 +20,11 @@ App::Rgit::Repository - Class representing a Git repository.
 
 =head1 VERSION
 
-Version 0.01
+Version 0.06
 
 =cut
 
-our $VERSION = '0.01';
+our $VERSION = '0.06';
 
 =head1 DESCRIPTION
 
@@ -38,43 +42,54 @@ If the C<fake> option is passed, C<$dir> isn't checked to be a valid C<git> 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;
- $dir = cwd       unless defined $dir;
+ if (defined $dir) {
+  $dir = abs_path $dir;
+ } else {
+  $dir = cwd;
+ }
+ $dir = canonpath $dir;
+
  my ($repo, $bare, $name, $work);
  if ($args{fake}) {
-  $repo = $dir;
- } else { 
-  my @tries = ($dir);
-  push @tries, "$dir.git" unless $dir =~ /\.git$/;
-  push @tries, catdir($dir, '.git') unless $dir eq '.git';
-  for (@tries) {
-   if (-d $_ && -d "$_/refs" and -d "$_/objects" and -e "$_/HEAD") {
-    $repo = $_;
-    last;
-   }
-  }
-  return unless defined $repo;
-  my @chunks = splitdir($repo);
-  my $last = pop @chunks;
+  $repo = $work = $dir;
+ } else {
+  return unless -d $dir
+            and -d "$dir/refs"
+            and -d "$dir/objects"
+            and -e "$dir/HEAD";
+
+  my @chunks = splitdir $dir;
+  my $last   = pop @chunks;
+  return unless defined $last;
+
   if ($last eq '.git') {
    $bare = 0;
    $name = $chunks[-1];
-   $work = catdir(@chunks);
-  } else {
+   $work = catdir @chunks;
+  } elsif ($last =~ /(.+)\.git$/) {
    $bare = 1;
-   ($name) = $last =~ /(.*)\.git$/;
-   $work = $repo;
+   $name = $1;
+   $work = 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<chdir>
@@ -85,9 +100,9 @@ C<chdir> into the repository's directory.
 
 sub chdir {
  my $self = shift;
- my $repo = $self->repo;
- chdir $repo or do {
-  warn "Couldn't chdir into $repo: $!";
+ my $dir = $self->work;
+ chdir $dir or do {
+  warn "Couldn't chdir into $dir: $!";
   return;
  };
  return 1;
@@ -107,26 +122,56 @@ sub _abs2rel {
  $a;
 }
 
+my %escapes = (
+ '%' => sub { '%' },
+ 'n' => sub { shift->name },
+ 'g' => sub { _abs2rel(shift->repo, shift->root) },
+ 'G' => sub { shift->repo },
+ '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)
+ },
+ 'B' => sub { $_[0]->bare ? $_[0]->repo : $_[0]->work . '.git' },
+ 'R' => sub { $_[1]->root },
+);
+my $e = quotemeta join '', keys %escapes;
+$e = "[$e]";
+
 sub run {
  my $self = shift;
  my $conf = shift;
  return unless $conf->isa('App::Rgit::Config');
  my @args = @_;
  unless ($self->fake) {
-  my %escapes = (
-   '^' => sub { '^' },
-   'n' => sub { $self->name },
-   'g' => sub { _abs2rel($self->repo, $conf->root) },
-   'G' => sub { $self->repo },
-   'w' => sub { _abs2rel($self->work, $conf->root) },
-   'W' => sub { $self->work },
-   'b' => sub { _abs2rel($self->bare ? $self->repo : $self->work . '.git', $conf->root) },
-   'B' => sub { $self->bare ? $self->repo : $self->work . '.git' },
-   'R' => sub { $conf->root },
-  );
-  s/\^([\^ngGwWbBR])/$escapes{$1}->()/eg for @args;
+  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;
  }
- system { $conf->git } $conf->git, @args;
+ if ($? == -1) {
+  $conf->crit("Failed to execute git: $!\n");
+  return;
+ }
+ my $ret;
+ $ret = WEXITSTATUS($?) if WIFEXITED($?);
+ my $sig;
+ if (WIFSIGNALED($?)) {
+  $sig = WTERMSIG($?);
+  $conf->warn("git died with signal $sig\n");
+  if ($sig == SIGINT || $sig == SIGQUIT) {
+   $conf->err("Aborting\n");
+   exit $sig;
+  }
+ } elsif ($ret) {
+  $conf->info("git returned $ret\n");
+ }
+ return wantarray ? ($ret, $sig) : $ret;
 }
 
 =head2 C<fake>
@@ -139,7 +184,13 @@ sub run {
 
 =head2 C<work>
 
-Accessors.
+Read-only accessors.
+
+=cut
+
+BEGIN {
+ eval "sub $_ { \$_[0]->{$_} }" for qw/fake repo bare name work/;
+}
 
 =head1 SEE ALSO
 
@@ -148,7 +199,7 @@ L<rgit>.
 =head1 AUTHOR
 
 Vincent Pit, C<< <perl at profvince.com> >>, L<http://profvince.com>.
-   
+
 You can contact me by mail or on C<irc.perl.org> (vincent).
 
 =head1 BUGS
@@ -163,7 +214,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.