Skip to content

Commit 3043655

Browse files
mwrockothatbrian
authored andcommitted
preserve former git environment variables
1 parent de1db54 commit 3043655

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

lib/git/lib.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,10 @@ def command_lines(cmd, opts = [], chdir = true, redirect = '')
734734
end
735735

736736
def command(cmd, opts = [], chdir = true, redirect = '', &block)
737+
old_git_dir = ENV['GIT_DIR']
738+
old_git_work_dir = ENV['GIT_WORK_TREE']
739+
old_git_index_file = ENV['GIT_INDEX_FILE']
740+
737741
ENV['GIT_DIR'] = @git_dir
738742
ENV['GIT_WORK_TREE'] = @git_work_dir
739743
ENV['GIT_INDEX_FILE'] = @git_index_file
@@ -762,6 +766,10 @@ def command(cmd, opts = [], chdir = true, redirect = '', &block)
762766
end
763767

764768
return output
769+
ensure
770+
ENV['GIT_DIR'] = old_git_dir
771+
ENV['GIT_WORK_TREE'] = old_git_work_dir
772+
ENV['GIT_INDEX_FILE'] = old_git_index_file
765773
end
766774

767775
# Takes the diff command line output (as Array) and parse it into a Hash

tests/units/test_lib.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ def test_log_commits
4949
assert_equal(20, a.size)
5050
end
5151

52+
def test_environment_reset
53+
ENV['GIT_DIR'] = '/my/git/dir'
54+
ENV['GIT_WORK_TREE'] = '/my/work/tree'
55+
ENV['GIT_INDEX_FILE'] = 'my_index'
56+
57+
@lib.log_commits :count => 10
58+
59+
assert_equal(ENV['GIT_DIR'], '/my/git/dir')
60+
assert_equal(ENV['GIT_WORK_TREE'], '/my/work/tree')
61+
assert_equal(ENV['GIT_INDEX_FILE'],'my_index')
62+
end
63+
5264
def test_revparse
5365
assert_equal('1cc8667014381e2788a94777532a788307f38d26', @lib.revparse('1cc8667014381')) # commit
5466
assert_equal('94c827875e2cadb8bc8d4cdd900f19aa9e8634c7', @lib.revparse('1cc8667014381^{tree}')) #tree

0 commit comments

Comments
 (0)