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.05';
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;
115 'n' => sub { shift->name },
116 'g' => sub { _abs2rel(shift->repo, shift->root) },
117 'G' => sub { shift->repo },
118 'w' => sub { _abs2rel(shift->work, shift->root) },
119 'W' => sub { shift->work },
121 my ($self, $conf) = @_;
122 _abs2rel($self->bare ? $self->repo : $self->work . '.git', $conf->root)
124 'B' => sub { $_[0]->bare ? $_[0]->repo : $_[0]->work . '.git' },
125 'R' => sub { $_[1]->root },
127 my $e = quotemeta join '', keys %escapes;
133 return unless $conf->isa('App::Rgit::Config');
135 unless ($self->fake) {
136 s/\^($e)/$escapes{$1}->($self, $conf)/eg for @args;
139 local $ENV{GIT_DIR} = $self->repo if exists $ENV{GIT_DIR};
140 local $ENV{GIT_EXEC_PATH} = $conf->git if exists $ENV{GIT_EXEC_PATH};
141 system { $conf->git } $conf->git, @args;
144 $conf->crit("Failed to execute git: $!\n");
148 $ret = WEXITSTATUS($?) if WIFEXITED($?);
150 if (WIFSIGNALED($?)) {
152 $conf->warn("git died with signal $sig\n");
153 if ($sig == SIGINT || $sig == SIGQUIT) {
154 $conf->err("Aborting\n");
158 $conf->info("git returned $ret\n");
160 return wantarray ? ($ret, $sig) : $ret;
181 Vincent Pit, C<< <perl at profvince.com> >>, L<http://profvince.com>.
183 You can contact me by mail or on C<irc.perl.org> (vincent).
187 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.
191 You can find documentation for this module with the perldoc command.
193 perldoc App::Rgit::Repository
195 =head1 COPYRIGHT & LICENSE
197 Copyright 2008 Vincent Pit, all rights reserved.
199 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
203 1; # End of App::Rgit::Repository