1 package App::Rgit::Repository;
7 use File::Spec (); # canonpath, catdir, splitdir, abs2rel
8 use POSIX (); # WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG SIGINT SIGQUIT
10 use App::Rgit::Utils (); # abs_path
12 my ($WIFEXITED, $WEXITSTATUS, $WIFSIGNALED, $WTERMSIG);
15 $WIFEXITED = eval { POSIX::WIFEXITED(0); 1 } ? \&POSIX::WIFEXITED
17 $WEXITSTATUS = eval { POSIX::WEXITSTATUS(0); 1 } ? \&POSIX::WEXITSTATUS
18 : sub { shift() >> 8 };
19 $WIFSIGNALED = eval { POSIX::WIFSIGNALED(0); 1 } ? \&POSIX::WIFSIGNALED
20 : sub { shift() & 127 };
21 $WTERMSIG = eval { POSIX::WTERMSIG(0); 1 } ? \&POSIX::WTERMSIG
22 : sub { shift() & 127 };
27 App::Rgit::Repository - Class representing a Git repository.
35 our $VERSION = '0.08';
39 Class representing a Git repository.
41 This is an internal class to L<rgit>.
47 my $arr = App::Rgit::Repository->new(
52 Creates a new repository starting from C<$dir>.
53 If the C<fake> option is passed, C<$dir> isn't checked to be a valid C<git> repository.
59 $class = ref $class || $class;
65 $dir = App::Rgit::Utils::abs_path($dir);
69 $dir = File::Spec->canonpath($dir);
71 my ($repo, $bare, $name, $work);
80 my @chunks = File::Spec->splitdir($dir);
81 my $last = pop @chunks;
82 return unless defined $last;
84 if (@chunks and $last eq '.git') {
87 $work = File::Spec->catdir(@chunks);
88 } elsif ($last =~ /(.+)\.git$/) {
91 $work = File::Spec->catdir(@chunks, $last);
100 fake => !!$args{fake},
110 C<chdir> into the repository's directory.
116 my $dir = $self->work;
118 warn "Couldn't chdir into $dir: $!";
126 my $code = $arr->run($conf, @args);
128 Runs C<git @args> on the repository for the L<App::Rgit::Config> configuration C<$conf>.
129 When the repository isn't fake, the format substitutions applies to C<@args> elements.
130 Returns the exit code.
135 my $a = File::Spec->abs2rel(@_);
136 $a = $_[0] unless defined $a;
142 'n' => sub { shift->name },
143 'g' => sub { $abs2rel->(shift->repo, shift->root) },
144 'G' => sub { shift->repo },
145 'w' => sub { $abs2rel->(shift->work, shift->root) },
146 'W' => sub { shift->work },
148 my ($self, $conf) = @_;
150 $self->bare ? $self->repo : $self->work . '.git',
154 'B' => sub { $_[0]->bare ? $_[0]->repo : $_[0]->work . '.git' },
155 'R' => sub { $_[1]->root },
157 my $e = quotemeta join '', keys %escapes;
163 return unless $conf->isa('App::Rgit::Config');
167 unless ($self->fake) {
168 s/%($e)/$escapes{$1}->($self, $conf)/eg for @args;
171 unshift @args, $conf->git;
172 $conf->info('Executing "', join(' ', @args), '" into ', $self->work, "\n");
175 local $ENV{GIT_DIR} = $self->repo if exists $ENV{GIT_DIR};
176 local $ENV{GIT_EXEC_PATH} = $conf->git if exists $ENV{GIT_EXEC_PATH};
177 system { $args[0] } @args;
181 $conf->crit("Failed to execute git: $!\n");
186 $ret = $WEXITSTATUS->($?) if $WIFEXITED->($?);
188 if ($WIFSIGNALED->($?)) {
189 $sig = $WTERMSIG->($?);
190 $conf->warn("git died with signal $sig\n");
191 if ($sig == POSIX::SIGINT() || $sig == POSIX::SIGQUIT()) {
192 $conf->err("Aborting\n");
196 $conf->info("git returned $ret\n");
199 return wantarray ? ($ret, $sig) : $ret;
217 eval "sub $_ { \$_[0]->{$_} }" for qw/fake repo bare name work/;
226 Vincent Pit, C<< <perl at profvince.com> >>, L<http://profvince.com>.
228 You can contact me by mail or on C<irc.perl.org> (vincent).
232 Please report any bugs or feature requests to C<bug-rgit at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=rgit>.
233 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
237 You can find documentation for this module with the perldoc command.
239 perldoc App::Rgit::Repository
241 =head1 COPYRIGHT & LICENSE
243 Copyright 2008,2009,2010 Vincent Pit, all rights reserved.
245 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
249 1; # End of App::Rgit::Repository