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>.
45 =head2 C<< new dir => $dir [, fake => 1 ] >>
47 Creates a new repository starting from C<$dir>.
48 If the C<fake> option is passed, C<$dir> isn't checked to be a valid C<git> repository.
54 $class = ref $class || $class;
60 $dir = App::Rgit::Utils::abs_path($dir);
64 $dir = File::Spec->canonpath($dir);
66 my ($repo, $bare, $name, $work);
75 my @chunks = File::Spec->splitdir($dir);
76 my $last = pop @chunks;
77 return unless defined $last;
79 if (@chunks and $last eq '.git') {
82 $work = File::Spec->catdir(@chunks);
83 } elsif ($last =~ /(.+)\.git$/) {
86 $work = File::Spec->catdir(@chunks, $last);
95 fake => !!$args{fake},
105 C<chdir> into the repository's directory.
111 my $dir = $self->work;
113 warn "Couldn't chdir into $dir: $!";
119 =head2 C<run $conf, @args>
121 Runs C<git @args> on the repository for the L<App::Rgit::Config> configuration C<$conf>.
122 When the repository isn't fake, the format substitutions applies to C<@args> elements.
123 Returns the exit code.
128 my $a = File::Spec->abs2rel(@_);
129 $a = $_[0] unless defined $a;
135 'n' => sub { shift->name },
136 'g' => sub { $abs2rel->(shift->repo, shift->root) },
137 'G' => sub { shift->repo },
138 'w' => sub { $abs2rel->(shift->work, shift->root) },
139 'W' => sub { shift->work },
141 my ($self, $conf) = @_;
143 $self->bare ? $self->repo : $self->work . '.git',
147 'B' => sub { $_[0]->bare ? $_[0]->repo : $_[0]->work . '.git' },
148 'R' => sub { $_[1]->root },
150 my $e = quotemeta join '', keys %escapes;
156 return unless $conf->isa('App::Rgit::Config');
160 unless ($self->fake) {
161 s/%($e)/$escapes{$1}->($self, $conf)/eg for @args;
164 unshift @args, $conf->git;
165 $conf->info('Executing "', join(' ', @args), '" into ', $self->work, "\n");
168 local $ENV{GIT_DIR} = $self->repo if exists $ENV{GIT_DIR};
169 local $ENV{GIT_EXEC_PATH} = $conf->git if exists $ENV{GIT_EXEC_PATH};
170 system { $args[0] } @args;
174 $conf->crit("Failed to execute git: $!\n");
179 $ret = $WEXITSTATUS->($?) if $WIFEXITED->($?);
181 if ($WIFSIGNALED->($?)) {
182 $sig = $WTERMSIG->($?);
183 $conf->warn("git died with signal $sig\n");
184 if ($sig == POSIX::SIGINT() || $sig == POSIX::SIGQUIT()) {
185 $conf->err("Aborting\n");
189 $conf->info("git returned $ret\n");
192 return wantarray ? ($ret, $sig) : $ret;
210 eval "sub $_ { \$_[0]->{$_} }" for qw/fake repo bare name work/;
219 Vincent Pit, C<< <perl at profvince.com> >>, L<http://profvince.com>.
221 You can contact me by mail or on C<irc.perl.org> (vincent).
225 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>.
226 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
230 You can find documentation for this module with the perldoc command.
232 perldoc App::Rgit::Repository
234 =head1 COPYRIGHT & LICENSE
236 Copyright 2008,2009,2010 Vincent Pit, all rights reserved.
238 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
242 1; # End of App::Rgit::Repository