Skip to content

Commit 4988861

Browse files
author
Salim Afiune
committed
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 <afiune@chef.io>
1 parent 9bd4407 commit 4988861

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ Git env config
6666
# If you need to use a custom SSH script
6767
config.git_ssh = '/path/to/ssh/script'
6868
end
69-
7069
```
7170

71+
_NOTE: Another way to specify where is the `git` binary is through the environment variable `GIT_PATH`_
7272

7373
Here are the operations that need read permission only.
7474

lib/git/config.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def initialize
1010
end
1111

1212
def binary_path
13-
@binary_path || 'git'
13+
@binary_path || ENV['GIT_PATH'] && File.join(ENV['GIT_PATH'], 'git') || 'git'
1414
end
1515

1616
def git_ssh

tests/units/test_config.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,27 @@ def test_set_config
3131
def test_env_config
3232
with_custom_env_variables do
3333
begin
34+
assert_equal(Git::Base.config.binary_path, 'git')
3435
assert_equal(Git::Base.config.git_ssh, nil)
36+
37+
ENV['GIT_PATH'] = '/env/bin'
3538
ENV['GIT_SSH'] = '/env/git/ssh'
3639

40+
assert_equal(Git::Base.config.binary_path, '/env/bin/git')
3741
assert_equal(Git::Base.config.git_ssh, '/env/git/ssh')
3842

3943
Git.configure do |config|
4044
config.binary_path = '/usr/bin/git'
4145
config.git_ssh = '/path/to/ssh/script'
4246
end
4347

48+
assert_equal(Git::Base.config.binary_path, '/usr/bin/git')
4449
assert_equal(Git::Base.config.git_ssh, '/path/to/ssh/script')
4550

4651
@git.log
4752
ensure
4853
ENV['GIT_SSH'] = nil
54+
ENV['GIT_PATH'] = nil
4955

5056
Git.configure do |config|
5157
config.binary_path = nil

0 commit comments

Comments
 (0)