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;
54 my ($repo, $bare, $name, $work);
59 my @chunks = splitdir $dir;
60 my $last = pop @chunks;
61 push @tries, "$dir.git" unless $last =~ /\.git$/;
62 push @tries, catdir($dir, '.git') unless $last eq '.git';
64 if (-d $_ && -d "$_/refs" and -d "$_/objects" and -e "$_/HEAD") {
69 return unless defined $repo;
70 $repo = canonpath $repo;
71 @chunks = splitdir $repo;
73 if ($last eq '.git') {
76 $work = catdir @chunks;
79 ($name) = $last =~ /(.*)\.git$/;
85 fake => !!$args{fake},
95 C<chdir> into the repository's directory.
101 my $dir = $self->work;
103 warn "Couldn't chdir into $dir: $!";
109 =head2 C<run $conf, @args>
111 Runs C<git @args> on the repository for the L<App::Rgit::Config> configuration C<$conf>.
112 When the repository isn't fake, the format substitutions applies to C<@args> elements.
113 Returns the exit code.
119 $a = $_[0] unless defined $a;
125 'n' => sub { shift->name },
126 'g' => sub { _abs2rel(shift->repo, shift->root) },
127 'G' => sub { shift->repo },
128 'w' => sub { _abs2rel(shift->work, shift->root) },
129 'W' => sub { shift->work },
131 my ($self, $conf) = @_;
132 _abs2rel($self->bare ? $self->repo : $self->work . '.git', $conf->root)
134 'B' => sub { $_[0]->bare ? $_[0]->repo : $_[0]->work . '.git' },
135 'R' => sub { $_[1]->root },
137 my $e = quotemeta join '', keys %escapes;
143 return unless $conf->isa('App::Rgit::Config');
145 unless ($self->fake) {
146 s/%($e)/$escapes{$1}->($self, $conf)/eg for @args;
148 unshift @args, $conf->git;
149 $conf->info('Executing "', join(' ', @args), '" into ', $self->work, "\n");
151 local $ENV{GIT_DIR} = $self->repo if exists $ENV{GIT_DIR};
152 local $ENV{GIT_EXEC_PATH} = $conf->git if exists $ENV{GIT_EXEC_PATH};
153 system { $args[0] } @args;
156 $conf->crit("Failed to execute git: $!\n");
160 $ret = WEXITSTATUS($?) if WIFEXITED($?);
162 if (WIFSIGNALED($?)) {
164 $conf->warn("git died with signal $sig\n");
165 if ($sig == SIGINT || $sig == SIGQUIT) {
166 $conf->err("Aborting\n");
170 $conf->info("git returned $ret\n");
172 return wantarray ? ($ret, $sig) : $ret;
190 eval "sub $_ { \$_[0]->{$_} }" for qw/fake repo bare name work/;
199 Vincent Pit, C<< <perl at profvince.com> >>, L<http://profvince.com>.
201 You can contact me by mail or on C<irc.perl.org> (vincent).
205 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.
209 You can find documentation for this module with the perldoc command.
211 perldoc App::Rgit::Repository
213 =head1 COPYRIGHT & LICENSE
215 Copyright 2008-2009 Vincent Pit, all rights reserved.
217 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
221 1; # End of App::Rgit::Repository