- my ($class, %args) = &validate;
- my $root = $args{root};
- return unless defined $root and -d $root;
- $root = abs_path $root unless file_name_is_absolute $root;
- return unless defined $args{git} and -x $args{git};
+ my $class = shift;
+ $class = ref $class || $class;
+
+ my %args = @_;
+
+ my $root = defined $args{root}
+ ? $args{root}
+ : defined $ENV{GIT_DIR}
+ ? $ENV{GIT_DIR}
+ : Cwd::cwd;
+ Carp::confess("Invalid root directory") unless -d $root;
+ $root = File::Spec->canonpath(App::Rgit::Utils::abs_path($root));
+
+ my $git;
+ my @candidates = (
+ defined $args{git}
+ ? $args{git}
+ : defined $ENV{GIT_EXEC_PATH}
+ ? $ENV{GIT_EXEC_PATH}
+ : map File::Spec->catfile($_, 'git'), File::Spec->path
+ );
+ if (IS_WIN32) {
+ my @acc;
+ for my $c (@candidates) {
+ push @acc, $c, map "$c.$_", qw/exe com bat cmd/;
+ }
+ @candidates = @acc;
+ }
+ for my $c (@candidates) {
+ if (-x $c) {
+ $git = $c;
+ last;
+ }
+ }
+ Carp::confess("Couldn't find a proper git executable") unless defined $git;
+ $git = File::Spec->canonpath(App::Rgit::Utils::abs_path($git));
+