Skip to content

Commit 4a969a0

Browse files
committed
Do not change ENV variables of a current process
Instead pass relevant environment to the actual shell command being run Signed-off-by: James Couball <jcouball@yahoo.com>
1 parent b0d89ac commit 4a969a0

File tree

2 files changed

+15
-58
lines changed

2 files changed

+15
-58
lines changed

lib/git/base.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ def self.root_of_worktree(working_dir)
6969
status = nil
7070

7171
git_cmd = "#{Git::Base.config.binary_path} -c core.quotePath=true -c color.ui=false rev-parse --show-toplevel 2>&1"
72-
result, status = Open3.capture2(git_cmd, chdir: File.expand_path(working_dir))
73-
result = result.chomp
72+
IO.popen(git_cmd, :chdir => working_dir) do |io|
73+
status = Process.wait2(io.pid).last
74+
result = io.read.chomp
75+
end
7476

7577
raise ArgumentError, "'#{working_dir}' is not in a git working tree" unless status.success?
7678
result

lib/git/lib.rb

Lines changed: 11 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
module Git
88
class Lib
99

10-
@@semaphore = Mutex.new
11-
1210
# The path to the Git working copy. The default is '"./.git"'.
1311
#
1412
# @return [Pathname] the path to the Git working copy.
@@ -442,10 +440,7 @@ def worktree_prune
442440
def list_files(ref_dir)
443441
dir = File.join(@git_dir, 'refs', ref_dir)
444442
files = []
445-
begin
446-
files = Dir.glob('**/*', base: dir).select { |f| File.file?(File.join(dir, f)) }
447-
rescue
448-
end
443+
files = Dir.glob(File.join(dir, '**/*')).select { |f| File.file?(f) } rescue nil
449444
files
450445
end
451446

@@ -1146,41 +1141,14 @@ def command_lines(cmd, *opts, chdir: nil)
11461141
op.split("\n")
11471142
end
11481143

1149-
# Takes the current git's system ENV variables and store them.
1150-
def store_git_system_env_variables
1151-
@git_system_env_variables = {}
1152-
ENV_VARIABLE_NAMES.each do |env_variable_name|
1153-
@git_system_env_variables[env_variable_name] = ENV[env_variable_name]
1154-
end
1155-
end
1156-
1157-
# Takes the previously stored git's ENV variables and set them again on ENV.
1158-
def restore_git_system_env_variables
1159-
ENV_VARIABLE_NAMES.each do |env_variable_name|
1160-
ENV[env_variable_name] = @git_system_env_variables[env_variable_name]
1161-
end
1162-
end
1163-
1164-
# Sets git's ENV variables to the custom values for the current instance.
1165-
def set_custom_git_env_variables
1166-
ENV['GIT_DIR'] = @git_dir
1167-
ENV['GIT_WORK_TREE'] = @git_work_dir
1168-
ENV['GIT_INDEX_FILE'] = @git_index_file
1169-
ENV['GIT_SSH'] = Git::Base.config.git_ssh
1170-
end
1171-
1172-
# Runs a block inside an environment with customized ENV variables.
1173-
# It restores the ENV after execution.
1174-
#
1175-
# @param [Proc] block block to be executed within the customized environment
1176-
def with_custom_env_variables(&block)
1177-
@@semaphore.synchronize do
1178-
store_git_system_env_variables()
1179-
set_custom_git_env_variables()
1180-
return block.call()
1181-
end
1182-
ensure
1183-
restore_git_system_env_variables()
1144+
# git's ENV variables for the current instance.
1145+
def custom_git_env_variables
1146+
{
1147+
'GIT_DIR' => @git_dir,
1148+
'GIT_WORK_TREE' => @git_work_dir,
1149+
'GIT_INDEX_FILE' => @git_index_file,
1150+
'GIT_SSH' => Git::Base.config.git_ssh,
1151+
}
11841152
end
11851153

11861154
def command(*cmd, redirect: '', chomp: true, chdir: nil, &block)
@@ -1200,18 +1168,7 @@ def command(*cmd, redirect: '', chomp: true, chdir: nil, &block)
12001168

12011169
git_cmd = "#{Git::Base.config.binary_path} #{global_opts} #{escaped_cmd} #{redirect} 2>&1"
12021170

1203-
output = nil
1204-
1205-
command_thread = nil;
1206-
1207-
status = nil
1208-
1209-
with_custom_env_variables do
1210-
command_thread = Thread.new do
1211-
output, status = run_command(git_cmd, chdir, &block)
1212-
end
1213-
command_thread.join
1214-
end
1171+
output, status = run_command(git_cmd, chdir, &block)
12151172

12161173
@logger.info(git_cmd)
12171174
@logger.debug(output)
@@ -1298,9 +1255,7 @@ def run_command(git_cmd, chdir=nil, &block)
12981255
opts = {}
12991256
opts[:chdir] = File.expand_path(chdir) if chdir
13001257

1301-
Open3.popen2(git_cmd, opts) do |stdin, stdout, wait_thr|
1302-
[block.call(stdout), wait_thr.value]
1303-
end
1258+
[IO.popen(custom_git_env_variables, git_cmd, opts, &block), $?]
13041259
end
13051260

13061261
def escape(s)

0 commit comments

Comments
 (0)