1 package App::Rgit::Repository;
6 use Cwd (); # cwd, abs_path
7 use File::Spec (); # canonpath, catdir, splitdir, abs2rel
8 use POSIX (); # WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG SIGINT SIGQUIT
10 my ($WIFEXITED, $WEXITSTATUS, $WIFSIGNALED, $WTERMSIG);
13 $WIFEXITED = eval { POSIX::WIFEXITED(0); 1 } ? \&POSIX::WIFEXITED
15 $WEXITSTATUS = eval { POSIX::WEXITSTATUS(0); 1 } ? \&POSIX::WEXITSTATUS
16 : sub { shift() >> 8 };
17 $WIFSIGNALED = eval { POSIX::WIFSIGNALED(0); 1 } ? \&POSIX::WIFSIGNALED
18 : sub { shift() & 127 };
19 $WTERMSIG = eval { POSIX::WTERMSIG(0); 1 } ? \&POSIX::WTERMSIG
20 : sub { shift() & 127 };
25 App::Rgit::Repository - Class representing a Git repository.
33 our $VERSION = '0.07';
37 Class representing a Git repository.
39 This is an internal class to L<rgit>.
43 =head2 C<< new dir => $dir [, fake => 1 ] >>
45 Creates a new repository starting from C<$dir>.
46 If the C<fake> option is passed, C<$dir> isn't checked to be a valid C<git> repository.
52 $class = ref $class || $class;
58 $dir = Cwd::abs_path($dir);
62 $dir = File::Spec->canonpath($dir);
64 my ($repo, $bare, $name, $work);
73 my @chunks = File::Spec->splitdir($dir);
74 my $last = pop @chunks;
75 return unless defined $last;
77 if (@chunks and $last eq '.git') {
80 $work = File::Spec->catdir(@chunks);
81 } elsif ($last =~ /(.+)\.git$/) {
84 $work = File::Spec->catdir(@chunks, $last);
93 fake => !!$args{fake},
103 C<chdir> into the repository's directory.
109 my $dir = $self->work;
111 warn "Couldn't chdir into $dir: $!";
117 =head2 C<run $conf, @args>
119 Runs C<git @args> on the repository for the L<App::Rgit::Config> configuration C<$conf>.
120 When the repository isn't fake, the format substitutions applies to C<@args> elements.
121 Returns the exit code.
126 my $a = File::Spec->abs2rel(@_);
127 $a = $_[0] unless defined $a;
133 'n' => sub { shift->name },
134 'g' => sub { $abs2rel->(shift->repo, shift->root) },
135 'G' => sub { shift->repo },
136 'w' => sub { $abs2rel->(shift->work, shift->root) },
137 'W' => sub { shift->work },
139 my ($self, $conf) = @_;
141 $self->bare ? $self->repo : $self->work . '.git',
145 'B' => sub { $_[0]->bare ? $_[0]->repo : $_[0]->work . '.git' },
146 'R' => sub { $_[1]->root },
148 my $e = quotemeta join '', keys %escapes;
154 return unless $conf->isa('App::Rgit::Config');
158 unless ($self->fake) {
159 s/%($e)/$escapes{$1}->($self, $conf)/eg for @args;
162 unshift @args, $conf->git;
163 $conf->info('Executing "', join(' ', @args), '" into ', $self->work, "\n");
166 local $ENV{GIT_DIR} = $self->repo if exists $ENV{GIT_DIR};
167 local $ENV{GIT_EXEC_PATH} = $conf->git if exists $ENV{GIT_EXEC_PATH};
168 system { $args[0] } @args;
172 $conf->crit("Failed to execute git: $!\n");
177 $ret = $WEXITSTATUS->($?) if $WIFEXITED->($?);
179 if ($WIFSIGNALED->($?)) {
180 $sig = $WTERMSIG->($?);
181 $conf->warn("git died with signal $sig\n");
182 if ($sig == POSIX::SIGINT() || $sig == POSIX::SIGQUIT()) {
183 $conf->err("Aborting\n");
187 $conf->info("git returned $ret\n");
190 return wantarray ? ($ret, $sig) : $ret;
208 eval "sub $_ { \$_[0]->{$_} }" for qw/fake repo bare name work/;
217 Vincent Pit, C<< <perl at profvince.com> >>, L<http://profvince.com>.
219 You can contact me by mail or on C<irc.perl.org> (vincent).
223 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>.
224 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
228 You can find documentation for this module with the perldoc command.
230 perldoc App::Rgit::Repository
232 =head1 COPYRIGHT & LICENSE
234 Copyright 2008,2009,2010 Vincent Pit, all rights reserved.
236 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
240 1; # End of App::Rgit::Repository