]> git.vpit.fr Git - perl/modules/rgit.git/blob - lib/App/Rgit/Config.pm
c384203a69d0cdac90ca12626ed0f3c4688b9b78
[perl/modules/rgit.git] / lib / App / Rgit / Config.pm
1 package App::Rgit::Config;
2
3 use strict;
4 use warnings;
5
6 use Carp       (); # confess
7 use Cwd        (); # cwd
8 use File::Spec (); # canonpath, catfile, path
9
10 use App::Rgit::Repository;
11 use App::Rgit::Utils qw/:levels/; # :levels, abs_path
12
13 use constant IS_WIN32 => $^O eq 'MSWin32';
14
15 =head1 NAME
16
17 App::Rgit::Config - Base class for App::Rgit configurations.
18
19 =head1 VERSION
20
21 Version 0.08
22
23 =cut
24
25 our $VERSION = '0.08';
26
27 =head1 DESCRIPTION
28
29 Base class for L<App::Rgit> configurations.
30
31 This is an internal class to L<rgit>.
32
33 =head1 METHODS
34
35 =head2 C<new>
36
37     my $arc = App::Rgit::Config->new(
38      root => $root,
39      git  => $git,
40     );
41
42 Creates a new configuration object based on the root directory C<$root> and using C<$git> as F<git> executable.
43
44 =cut
45
46 sub new {
47  my $class = shift;
48  $class = ref $class || $class;
49
50  my %args = @_;
51
52  my $root = defined $args{root}
53               ? $args{root}
54               : defined $ENV{GIT_DIR}
55                   ? $ENV{GIT_DIR}
56                   : Cwd::cwd;
57  Carp::confess("Invalid root directory") unless -d $root;
58  $root = File::Spec->canonpath(App::Rgit::Utils::abs_path($root));
59
60  my $git;
61  my @candidates = (
62   defined $args{git}
63     ? $args{git}
64     : defined $ENV{GIT_EXEC_PATH}
65         ? $ENV{GIT_EXEC_PATH}
66         : map File::Spec->catfile($_, 'git'), File::Spec->path
67  );
68  if (IS_WIN32) {
69   my @acc;
70   for my $c (@candidates) {
71    push @acc, $c, map "$c.$_", qw/exe com bat cmd/;
72   }
73   @candidates = @acc;
74  }
75  for my $c (@candidates) {
76   if (-x $c) {
77    $git = $c;
78    last;
79   }
80  }
81  Carp::confess("Couldn't find a proper git executable") unless defined $git;
82  $git = File::Spec->canonpath(App::Rgit::Utils::abs_path($git));
83
84  my $conf = 'App::Rgit::Config::Default';
85  eval "require $conf; 1" or Carp::confess("Couldn't load $conf: $@");
86
87  my $r = App::Rgit::Repository->new(fake => 1);
88  return unless defined $r;
89
90  bless {
91   root     => $root,
92   git      => $git,
93   cwd_repo => $r,
94   debug    => defined $args{debug} ? int $args{debug} : WARN,
95  }, $conf;
96 }
97
98 =head2 C<info>
99
100     $arr->info($msg);
101
102 =head2 C<warn>
103
104     $arr->warn($msg);
105
106 =head2 C<err>
107
108     $arr->err($msg);
109
110 =head2 C<crit>
111
112     $arr->crit($msg);
113
114 Notifies a message C<$msg> of the corresponding level.
115
116 =cut
117
118 sub _notify {
119  my $self = shift;
120  my $level = shift;
121  if ($self->debug >= $level) {
122   print STDERR @_;
123   return 1;
124  }
125  return 0;
126 }
127
128 sub info { shift->_notify(INFO, @_) }
129
130 sub warn { shift->_notify(WARN, @_) }
131
132 sub err  { shift->_notify(ERR, @_) }
133
134 sub crit { shift->_notify(CRIT, @_) }
135
136 =head2 C<root>
137
138 =head2 C<git>
139
140 =head2 C<repos>
141
142 =head2 C<cwd_repo>
143
144 =head2 C<debug>
145
146 Read-only accessors.
147
148 =cut
149
150 BEGIN {
151  eval "sub $_ { \$_[0]->{$_} }" for qw/root git cwd_repo debug/;
152 }
153
154 =head1 SEE ALSO
155
156 L<rgit>.
157
158 =head1 AUTHOR
159
160 Vincent Pit, C<< <perl at profvince.com> >>, L<http://profvince.com>.
161
162 You can contact me by mail or on C<irc.perl.org> (vincent).
163
164 =head1 BUGS
165
166 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>.
167 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
168
169 =head1 SUPPORT
170
171 You can find documentation for this module with the perldoc command.
172
173     perldoc App::Rgit::Config
174
175 =head1 COPYRIGHT & LICENSE
176
177 Copyright 2008,2009,2010 Vincent Pit, all rights reserved.
178
179 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
180
181 =cut
182
183 1; # End of App::Rgit::Config