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