From 4988861799daf1cdf289d47bffe62eb641e86a9a Mon Sep 17 00:00:00 2001 From: Salim Afiune Date: Fri, 2 Aug 2019 12:55:33 +0200 Subject: [PATCH] Allow consumers to point git binary via env var By adding a new environment variable called `GIT_PATH` we can allow consumers, that is, a user of a gem that the gem itself uses the git gem, to customize the location of the git binary. Example: Having a gem called `git-tool` that uses this gem `git`, if I, as a user wants to modify the git bin location, I could do: ``` GIT_PATH=/foo/bin git-tool bar ``` Signed-off-by: Salim Afiune --- README.md | 2 +- lib/git/config.rb | 2 +- tests/units/test_config.rb | 6 ++++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bc82b9e0..32aeca6d 100644 --- a/README.md +++ b/README.md @@ -66,9 +66,9 @@ Git env config # If you need to use a custom SSH script config.git_ssh = '/path/to/ssh/script' end - ``` +_NOTE: Another way to specify where is the `git` binary is through the environment variable `GIT_PATH`_ Here are the operations that need read permission only. diff --git a/lib/git/config.rb b/lib/git/config.rb index a4a90e51..4fefe454 100644 --- a/lib/git/config.rb +++ b/lib/git/config.rb @@ -10,7 +10,7 @@ def initialize end def binary_path - @binary_path || 'git' + @binary_path || ENV['GIT_PATH'] && File.join(ENV['GIT_PATH'], 'git') || 'git' end def git_ssh diff --git a/tests/units/test_config.rb b/tests/units/test_config.rb index 7721ba24..a1753831 100644 --- a/tests/units/test_config.rb +++ b/tests/units/test_config.rb @@ -31,9 +31,13 @@ def test_set_config def test_env_config with_custom_env_variables do begin + assert_equal(Git::Base.config.binary_path, 'git') assert_equal(Git::Base.config.git_ssh, nil) + + ENV['GIT_PATH'] = '/env/bin' ENV['GIT_SSH'] = '/env/git/ssh' + assert_equal(Git::Base.config.binary_path, '/env/bin/git') assert_equal(Git::Base.config.git_ssh, '/env/git/ssh') Git.configure do |config| @@ -41,11 +45,13 @@ def test_env_config config.git_ssh = '/path/to/ssh/script' end + assert_equal(Git::Base.config.binary_path, '/usr/bin/git') assert_equal(Git::Base.config.git_ssh, '/path/to/ssh/script') @git.log ensure ENV['GIT_SSH'] = nil + ENV['GIT_PATH'] = nil Git.configure do |config| config.binary_path = nil