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