1 package App::Rgit::Repository;
6 use Cwd qw/cwd abs_path/;
7 use File::Spec::Functions qw/canonpath catdir splitdir abs2rel file_name_is_absolute/;
8 use POSIX qw/WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG SIGINT SIGQUIT/;
11 no warnings 'redefine';
12 *WIFEXITED = sub { 1 } unless eval { WIFEXITED(0); 1 };
13 *WEXITSTATUS = sub { shift() >> 8 } unless eval { WEXITSTATUS(0); 1 };
14 *WIFSIGNALED = sub { shift() & 127 } unless eval { WIFSIGNALED(0); 1 };
19 App::Rgit::Repository - Class representing a Git repository.
27 our $VERSION = '0.06';
31 Class representing a Git repository.
33 This is an internal class to L<rgit>.
37 =head2 C<< new dir => $dir [, fake => 1 ] >>
39 Creates a new repository starting from C<$dir>.
40 If the C<fake> option is passed, C<$dir> isn't checked to be a valid C<git> repository.
46 $class = ref $class || $class;
51 $dir = abs_path $dir if defined $dir and not file_name_is_absolute $dir;
52 $dir = cwd unless defined $dir;
53 $dir = canonpath $dir;
55 my ($repo, $bare, $name, $work);
64 my @chunks = splitdir $dir;
65 my $last = pop @chunks;
66 return unless defined $last;
68 if ($last eq '.git') {
71 $work = catdir @chunks;
72 } elsif ($last =~ /(.+)\.git$/) {
75 $work = catdir @chunks, $last;
84 fake => !!$args{fake},
94 C<chdir> into the repository's directory.
100 my $dir = $self->work;
102 warn "Couldn't chdir into $dir: $!";
108 =head2 C<run $conf, @args>
110 Runs C<git @args> on the repository for the L<App::Rgit::Config> configuration C<$conf>.
111 When the repository isn't fake, the format substitutions applies to C<@args> elements.
112 Returns the exit code.
118 $a = $_[0] unless defined $a;
124 'n' => sub { shift->name },
125 'g' => sub { _abs2rel(shift->repo, shift->root) },
126 'G' => sub { shift->repo },
127 'w' => sub { _abs2rel(shift->work, shift->root) },
128 'W' => sub { shift->work },
130 my ($self, $conf) = @_;
131 _abs2rel($self->bare ? $self->repo : $self->work . '.git', $conf->root)
133 'B' => sub { $_[0]->bare ? $_[0]->repo : $_[0]->work . '.git' },
134 'R' => sub { $_[1]->root },
136 my $e = quotemeta join '', keys %escapes;
142 return unless $conf->isa('App::Rgit::Config');
144 unless ($self->fake) {
145 s/%($e)/$escapes{$1}->($self, $conf)/eg for @args;
147 unshift @args, $conf->git;
148 $conf->info('Executing "', join(' ', @args), '" into ', $self->work, "\n");
150 local $ENV{GIT_DIR} = $self->repo if exists $ENV{GIT_DIR};
151 local $ENV{GIT_EXEC_PATH} = $conf->git if exists $ENV{GIT_EXEC_PATH};
152 system { $args[0] } @args;
155 $conf->crit("Failed to execute git: $!\n");
159 $ret = WEXITSTATUS($?) if WIFEXITED($?);
161 if (WIFSIGNALED($?)) {
163 $conf->warn("git died with signal $sig\n");
164 if ($sig == SIGINT || $sig == SIGQUIT) {
165 $conf->err("Aborting\n");
169 $conf->info("git returned $ret\n");
171 return wantarray ? ($ret, $sig) : $ret;
189 eval "sub $_ { \$_[0]->{$_} }" for qw/fake repo bare name work/;
198 Vincent Pit, C<< <perl at profvince.com> >>, L<http://profvince.com>.
200 You can contact me by mail or on C<irc.perl.org> (vincent).
204 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>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
208 You can find documentation for this module with the perldoc command.
210 perldoc App::Rgit::Repository
212 =head1 COPYRIGHT & LICENSE
214 Copyright 2008-2009 Vincent Pit, all rights reserved.
216 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
220 1; # End of App::Rgit::Repository