From: Vincent Pit Date: Wed, 24 Feb 2010 18:14:27 +0000 (+0100) Subject: Make App::Rgit::Config->new discover the git executable itself X-Git-Tag: v0.07~4 X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2Frgit.git;a=commitdiff_plain;h=1df4b5b8d8d71567094a0c6ec3702ab71b614bd9 Make App::Rgit::Config->new discover the git executable itself --- diff --git a/bin/rgit b/bin/rgit index 98510c2..19558a3 100755 --- a/bin/rgit +++ b/bin/rgit @@ -3,10 +3,9 @@ use strict; use warnings; -use Carp qw/croak/; -use Config qw/%Config/; -use Cwd qw/cwd/; -use File::Spec (); # catfile, path +use Carp qw/croak/; +use Config qw/%Config/; +use Cwd qw/cwd/; use App::Rgit; use App::Rgit::Utils qw/:levels/; @@ -44,23 +43,11 @@ if (not defined $policy) { setpgrp 0, 0 if $Config{d_setpgrp}; -my $git = $ENV{GIT_EXEC_PATH}; -unless (defined $git) { - for (File::Spec->path) { - my $g = File::Spec->catfile($_, 'git'); - if (-x $g) { - $git = $g; - last; - } - } -} -croak "Couldn't find any valid git executable" unless defined $git; - my $root = $ENV{GIT_DIR}; $root = cwd unless defined $root; my $ar = App::Rgit->new( - git => $git, + git => undef, # Autodiscovery root => $root, cmd => $cmd, args => \@ARGV, diff --git a/lib/App/Rgit/Config.pm b/lib/App/Rgit/Config.pm index 0442966..94a3359 100644 --- a/lib/App/Rgit/Config.pm +++ b/lib/App/Rgit/Config.pm @@ -5,7 +5,7 @@ use warnings; use Carp (); # confess use Cwd (); # abs_path -use File::Spec (); # canonpath +use File::Spec (); # canonpath, catfile, path use App::Rgit::Repository; use App::Rgit::Utils qw/:levels/; @@ -48,16 +48,28 @@ sub new { return unless defined $root and -d $root; $root = File::Spec->canonpath(Cwd::abs_path($root)); - my $git = $args{git}; - return unless defined $git; + 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) { - unless (-x $git) { - $git .= '.bat'; - return unless -x $git; + my @acc; + for my $c (@candidates) { + push @acc, $c, map "$c.$_", qw/exe com bat cmd/; } - } else { - return unless -x $git; + @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(Cwd::abs_path($git)); my $conf = 'App::Rgit::Config::Default'; diff --git a/t/15-failures.t b/t/15-failures.t index 7652989..0e31edd 100644 --- a/t/15-failures.t +++ b/t/15-failures.t @@ -6,7 +6,7 @@ use warnings; use Cwd (); # cwd use File::Spec (); # catdir -use Test::More tests => 42; +use Test::More tests => 43; use App::Rgit; @@ -27,12 +27,24 @@ is $@, '', "App::Rgit->new(): wrong root: doesn't croak"; is $res, undef, 'App::Rgit->new(): wrong root: returns undef'; $res = eval { + local $ENV{GIT_EXEC_PATH}; + local $ENV{PATH} = 't/bin'; App::Rgit->new( root => 't', ); }; -is $@, '', "App::Rgit->new(): no git: doesn't croak"; -is $res, undef, 'App::Rgit->new(): no git: returns undef'; +is $@, '', "App::Rgit->new(): no git, no GIT_EXEC_PATH: doesn't croak"; +isa_ok $res, 'App::Rgit', + 'App::Rgit->new(): no git, no GIT_EXEC_PATH: returns object'; + +$res = eval { + local $ENV{GIT_EXEC_PATH} = 't/bin/git'; + App::Rgit->new( + root => 't', + ); +}; +is $@, '', "App::Rgit->new(): no git: doesn't croak"; +isa_ok $res, 'App::Rgit', 'App::Rgit->new(): no git: returns object'; $res = eval { App::Rgit->new( @@ -40,8 +52,8 @@ $res = eval { git => $0, ); }; -is $@, '', "App::Rgit->new(): wrong git: doesn't croak"; -is $res, undef, 'App::Rgit->new(): wrong git: returns undef'; +like $@, qr/Couldn't find a proper git executable/, + 'App::Rgit->new(): wrong git: croaks'; $res = eval { App::Rgit->new(