1 package App::Rgit::Repository;
6 use Cwd qw/cwd abs_path/;
7 use File::Spec::Functions qw/catdir splitdir abs2rel file_name_is_absolute/;
8 use POSIX qw/WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG SIGINT SIGQUIT/;
10 use Object::Tiny qw/fake repo bare name work/;
12 use App::Rgit::Utils qw/validate/;
16 App::Rgit::Repository - Class representing a Git repository.
24 our $VERSION = '0.04';
28 Class representing a Git repository.
30 This is an internal class to L<rgit>.
34 =head2 C<< new dir => $dir [, fake => 1 ] >>
36 Creates a new repository starting from C<$dir>.
37 If the C<fake> option is passed, C<$dir> isn't checked to be a valid C<git> repository.
42 my ($class, %args) = &validate;
44 $dir = abs_path $dir if defined $dir and not file_name_is_absolute $dir;
45 $dir = cwd unless defined $dir;
46 my ($repo, $bare, $name, $work);
51 my @chunks = splitdir $dir;
52 my $last = pop @chunks;
53 push @tries, "$dir.git" unless $last =~ /\.git$/;
54 push @tries, catdir($dir, '.git') unless $last eq '.git';
56 if (-d $_ && -d "$_/refs" and -d "$_/objects" and -e "$_/HEAD") {
61 return unless defined $repo;
62 @chunks = splitdir $repo;
64 if ($last eq '.git') {
67 $work = catdir @chunks;
70 ($name) = $last =~ /(.*)\.git$/;
75 fake => !!$args{fake},
85 C<chdir> into the repository's directory.
91 my $dir = $self->work;
93 warn "Couldn't chdir into $dir: $!";
99 =head2 C<run $conf, @args>
101 Runs C<git @args> on the repository for the L<App::Rgit::Config> configuration C<$conf>.
102 When the repository isn't fake, the format substitutions applies to C<@args> elements.
103 Returns the exit code.
109 $a = $_[0] unless defined $a;
116 return unless $conf->isa('App::Rgit::Config');
118 unless ($self->fake) {
121 'n' => sub { $self->name },
122 'g' => sub { _abs2rel($self->repo, $conf->root) },
123 'G' => sub { $self->repo },
124 'w' => sub { _abs2rel($self->work, $conf->root) },
125 'W' => sub { $self->work },
126 'b' => sub { _abs2rel($self->bare ? $self->repo : $self->work . '.git', $conf->root) },
127 'B' => sub { $self->bare ? $self->repo : $self->work . '.git' },
128 'R' => sub { $conf->root },
130 s/\^([\^ngGwWbBR])/$escapes{$1}->()/eg for @args;
133 local $ENV{GIT_DIR} = $self->repo if exists $ENV{GIT_DIR};
134 local $ENV{GIT_EXEC_PATH} = $conf->git if exists $ENV{GIT_EXEC_PATH};
135 system { $conf->git } $conf->git, @args;
138 warn "Failed to execute git: $!\n";
142 $ret = WEXITSTATUS($?) if WIFEXITED($?);
144 if (WIFSIGNALED($?)) {
146 warn "git died with signal $sig\n";
147 if ($sig == SIGINT || $sig == SIGQUIT) {
152 warn "git returned $ret\n";
154 return wantarray ? ($ret, $sig) : $ret;
175 Vincent Pit, C<< <perl at profvince.com> >>, L<http://profvince.com>.
177 You can contact me by mail or on C<irc.perl.org> (vincent).
181 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.
185 You can find documentation for this module with the perldoc command.
187 perldoc App::Rgit::Repository
189 =head1 COPYRIGHT & LICENSE
191 Copyright 2008 Vincent Pit, all rights reserved.
193 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
197 1; # End of App::Rgit::Repository